0

I have the following JQuery in my aspx page to call the Code behind function

onDelete: function (item) {
      //Some Code
       $.ajax({
           type: "POST",
           url: 'Page_Name.aspx/callmethod',
           data: "{}",
           contentType: "application/json; charset=utf-8",
           dataType: "json"
       });
    }

I am calling the below Method

[WebMethod]
public static void callmethod()
{
    Page_Name call = new Page_Name();
    call.Function();
    call.Function_Structure();
    call.Function_items_Structure();
    call.OClear();
    call.PClear();
    call.IClear();
}

I had tried in many ways but its not working kindly any one point me what could be wrong here

10
  • have you checked console for errors? Commented May 26, 2014 at 6:10
  • Not sure, but is it valid to specify a return datatype, but not return anything? I'd expect your ajax call to complain about such a thing. Commented May 26, 2014 at 7:11
  • Also, do I understand correctly that by call.pnlCH.Visible = false;, you are trying to set the ASP.Net form element's attributes via an Ajax call? That won't work. If ASP.Net needs to re-render the controls, you'll need a postback, not an Ajax call. Commented May 26, 2014 at 7:13
  • I had removed the call.pnlCH.Visible = false; call.pnlPdtl.Visible = false; in my code Commented May 26, 2014 at 8:43
  • 1
    @SurajSingh I had accepted your Answer Commented May 26, 2014 at 9:26

3 Answers 3

2

Try using {} for your data, "{}" will be considered as string or don't include data parameter if you are not sending any parameters.

onDelete: function (item) {
      //Some Code
       $.ajax({
           type: "POST",
           url: 'Page_Name.aspx/callmethod',
          // data: "{}",
              data: {}, 
           contentType: "application/json; charset=utf-8",
           dataType: "json"
       });
    }

Here

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

Comments

2

What you are trying to accomplish is inherently impossible. In your webmethod, I see the following lines:

call.pnlCH.Visible = false;
call.pnlPdtl.Visible = false;

You're trying to change the attributes of your ASP.Net form elements, but without using a postback operation. If you do not have a postback, ASP.Net will not re-render your page for you! It will only return what you tell the webmethod to return, which in your case is void.

There are two ways to fix this, I'm not sure which it is you want:

Option 1 - No ASP.Net interference needed

If you only want to change some element's visibility, you could do that via jQuery, you don't need your backend for that.

$("#myElement").show();
$("#myElement").hide();

For ASP.Net controls, you'll want to know the clientId.

var clientId = <%= pnlCH.ClientID %>;

$("#" + clientID).hide();

Option 2 - ASP.Net is needed

If you need your backend code for this (e.g. to look something up from the database, or any other good reason), doing it purely via jQuery isn't going to help. In this case, what you'll want to be doing is perform a regular postback scenario.

Having the page post back means that ASP.Net will render your page again. Any change you make to the form elements will then be rendered.

It's important to note here that a postback is required to have ASP.Net re-render your page.

So all in all, I'm not sure which way you want/need to go with this. But your current approach, while syntactically valid, will simply not work because of how ASP.Net and client-side asynchronous calls work.

Comments

0

Try adding an error function to your $ajax call, and see what errors come back.

data: {},
dataType: "json",
error: function(jqXHR, textStatus, errorThrown ) {
   // debug here
   alert(jqXHR);
},
success: function() ... 

1 Comment

The Error I am getting is Internal Server Error

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.