Prabath Yapa
Prabath Yapa

Reputation: 1229

Use asp.net AJAX from javascript

This is probably a simple question. I use ASP.NET ajax toolkit and Jquery. I want to call a server-side function/method from Javascript and have it update a control. Can i do this?

Client-side

send_request(foobar,args);

Server-side

private void foorbar(){
Label1.Text = "updated";
}

Upvotes: 0

Views: 105

Answers (5)

bugventure
bugventure

Reputation: 498

@smkngspcmn:

I placed everything inside an update panel and did something like $('#Year').change(function() { __doPostBack("submit", ""); }); That does do a full post back without Ajax. What am i doing wrong? Should i place the above script inside the update panel as well?

The first argument to __doPostBack() should be the UniqueID of a server-side control inside the UpdatePanel. For example, you can put a hidden button inside the UpdatePanel:

<asp:Button ID="HiddenButton" runat="server" 
    style="display:none" OnClick="HiddenButton_Click" />

When the button is rendered on the page, you can take the name attribute of the <input type="submit"> element that represents the submit button and use it for the first argument to _doPostBack(). In this way, whenever your script runs it the UpdatePanel will make an asynchronous postback and the HiddenButton_Click event handler will be fired.

Upvotes: 0

Anil Namde
Anil Namde

Reputation: 6608

Yes that can be done Client Callback

Upvotes: 0

drhanlau
drhanlau

Reputation: 2527

Do you want it to fire a server-side method and update a server-side control on the page? You can create a ASP.NET UpdatePanel, let's say there is a button1 inside, and from your JQuery code, write this.

function OnClick()
{
   __doPostBack(button1.ClientID, "argument")
}

and in your server side code, Page_Load event, you will find the EVENTTARGET and EVENTARGUMENT in the REQUEST variable, which contains the information you just postback, you can then update the control in the UpdatePanel itself as long as the control is within the UpdatePanel, it will be handled properly by ASP.NET AJAX.

More details here http://www.dotnetspider.com/resources/16920-Post-back-gets-demystified-doPostBack-defined.aspx

Upvotes: 1

Praveen Prasad
Praveen Prasad

Reputation: 32107

jQuery.ajax({
   url:'url to call', //usually webservices in asp.net
   dataType:'json',
   type:'POST', //(asp.net werbservices by default accepts POST request only)
   success:function(data){
       //do some thing with this data, like will dom elements etc
    }
});

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

You could take a look at ASP.NET Page Methods.

Upvotes: 0

Related Questions