ASP.NET – Execute server side code from JavaScript using Script Manager

We all come across to such situation where you need to call server side code from JavaScript, not using jQuery AJAX though, without allowing form to post back to it. Further posting was required to capture data and save it into the database as well.

So we at GoodCore had to find some trigger that can post form data to save record into the database. I was using ImageButton due to design requirements. If you specify a value for the PostBackUrl property, the page does not post back to itself. In my case it had to specify one.
So this post is an effort to save your time.

Ok, let’s get started.

Here are few easy steps we will be doing to get this done: (can’t miss any of them)

1. Create a JavaScript method. I will name it “ServerSideMethod”. Here is the code:

function ServerSideMethod()





2. PageMethods is something most of you might be familiar with or even heard of. We need to set “EnablePageMethods” to true in ScriptManager.

<asp:ScriptManager ID=”sm” runat=”server” EnablePageMethods=”true” />

3. Now we will define our server side method which actually does the trick, saving record in database in my case:


public static void CompleteOrder()




We must add method attribute [System.Web.Services.WebMethod] before we define our method else the method call will fail from JavaScript.

4. Finally, we need to add an attribute to our form; here my form id is “myform”:

this.myform.Attributes.Add(“onsubmit”, “ServerSideMethod()”);

This will trigger our JavaScript method at the form is submitted. This line can be added in Page_Load event handler.

  1. No comments yet.

Leave a reply


+ four = 13