views:

38

answers:

5

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";
}
A: 

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

Darin Dimitrov
A: 
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
    }
});
Praveen Prasad
+1  A: 

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

cherhan
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?
smkngspcmn
A: 

Yes that can be done Client Callback

Anil Namde
A: 

@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.

bugventure