1

Due to issues with styling, and needing multiple forms on one single web page, I currently have a form that is outside of the normal form with runat=server.

Is it possible for me to still call a WebMethod in the C# codebehind of this web page using ajax? I want to submit the information from this form to the same connection string that I am using earlier in the page, in a different form.

Here is my current code:

$().ready(function () {
    $("input[type=submit]").click(function () {
        handleClick();
        createJob();
    });
});

function createJob() {
var txtTestValue = document.getElementById('jobTitle').value;

$.ajax({
    // POST signals a data request
    type: "POST",
    // This directs which function in the c# code behind to use
    url: "Account/help.aspx/CreateJob",
    // The paramater pageIndex, page number we need to load, to pass to GetCustomers(int pageIndex)
    data: txtTestValue,
    // Type of data we are sending to the server (i.e. the pageIndex paramater)
    contentType: "application/json; charset=utf-8",
    // Type of data we expect back from the server (to fill into the html ultimately)
    dataType: "text",
    // If all goes smoothly to here, run the function that fills our html table
    success: OnSuccess,
    // On failure, error alert user (aka me so that I know something isn't working)
    failure: function (response) {
        alert("failure");
    },
    error: function (response) {
        alert("error");
    }
});
});

And my WebMethod in the codebehind:

[WebMethod]
public string CreateJob()
{
    //rest of my database code here
}

Sorry for the confusion but it's doing everything right up until the ajax code, and then seems to ignore it (and returns that the ajax failed with an error). My code is not reaching the WebMethod and any breakpoints I set in Visual Studio are not triggered in the page header. Thanks in advance for your help!

6
  • What class is the CreateJob method a part of? The convention for URL path for web methods in ASP.NET is Class/Method. Commented Jan 18, 2017 at 19:41
  • 1
    It is. Use the Developer Tools to inspect a WebMethod request in a "normal" context. Do this same thing with AJAX. If there is an error the requests has not been emulated correctly. Commented Jan 18, 2017 at 19:42
  • I bet you are getting a 404 response and "Account/help.aspx/CreateJob" is not resolving to where you think it is resolving. Commented Jan 18, 2017 at 19:47
  • @maniak1982 it's a public partial class gethelp that inherits from the Page class, and then the aspx side links to my page help.aspx.cs, but also inherits from the gethelp class...could it be that I have a page named help and they inherit from a gethelp class that's messing it up? (i.e. Change the names of everything to be the same) Commented Jan 18, 2017 at 19:54
  • @user2864740 Thank you! Where can I find the developer tools? Or is there a quick tutorial on how to do this? I've never used these before Commented Jan 18, 2017 at 19:55

2 Answers 2

2

You need to declare the method as static.

[WebMethod]
public static string CreateJob()
       ^^^^^
{
    //rest of my database code here
}

Another issue is if input[type=submit] is ASP.Net Button control, it will post back to server. You cannot use ASP.Net Server control to make jQuery Ajax call - $.ajax.

// This code won't work if `input[type=submit]` is a server button control
$(function () {
    $("input[type=submit]").click(function () {
        handleClick();
        createJob();
    });
});

you need to use regular html input or button control with type=button instead of type=submit.

Sign up to request clarification or add additional context in comments.

6 Comments

Did the trick thank you! One more quick question, my previous codebehind wasn't static because the name of the user is recorded and submitted when logged in. Is there a way around this to still allow recording the user name while also keeping the WebMethod static? Thanks again
I'm not sure I understand your comment, However, if you need session, you will need [WebMethod(EnableSession = true)]. If not, could you create a new question?
@StevenPfeifer Session-transient data should be shared via Session State. There is no running instance of the Page after each response: it is created and destroyed for each request. Using static variables may appear to "work" in some cases but this approach is unreliable (eg. won't work cross-appdomain) and prone to maintaining invalid state between interleaved requests.
@StevenPfeifer "Re-authenticate a user?" The Session can be used to store if a user is authenticated - usually something like the "user id" would be stored, where only authenticated / logged in / valid users have such set. This can be checked in the WebMethod along with all the other session information that is transmitted. The WebMethod itself does nothing with authentication. Session state is generally considered 'secure'; with the assumption the session nonce-cookie has not been intercepted/stolen (see CSRF etc. for additional precautions when relevant).
@StevenPfeifer Basic precautions for session state usage is http-only cookies (the default in .NET's built-in session state provider) and HTTPS, blah blah.
|
1

The webmethod should be static.

[WebMethod]
public static string CreateJob()
{
    //rest of my database code here
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.