PostBack call form javascript

It is quite amazing to note that only two of the ASP.NET web server controls cause a postback. All the other controls use the JavaScript __doPostBack function to trigger the postback. In this article you will learn about the __doPostBack function and how it works. Postback is built into the ASP.NET and most of the web controls support it without writing any code. There may be some scenario where you may want to explicitly postback to the server using some clientside javascript. It is prett

function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;

all you have to do is, just call this method with appropriate arguments. You may call this as shown below:



<script language='Javascript'>
__doPostBack('__Page', 'MyCustomArgument');


However, it is not recommended to use this method name directly in the client side. The best approach is, generate this piece of code from the code behind file using ASP.NET. This way, you are safe even if Microsoft later change the name of the method '__doPostBack' to something else in a future release.

In your code behind file, declare a protected variable as shown below:

Protected PostBackStr As String

Now, in the page load event, write the following code:

PostBackStr = Page.ClientScript.GetPostBackEventReference(Me, "MyCustomArgument")

The method GetPostBackEventReference() will generate the same piece of client side code that you need to use to call the Postback method. Instead of harcoding the method name __doPostBack, we are asking ASP.NET to tell us what is the method name.

Now insert the following code in your Aspx page:

<script language='Javascript'>
<%= PostBackStr %>

At runtime, it will be evaluated as

<script language='Javascript'>
__doPostBack('__Page', 'MyCustomArgument');

Remember to insert the above script into some Javascript method/event where you want to call the postback, instead of simply inserting into the page as shown above.


handle the postback in code behind

You found how to call the postback from javascript. Now you need a way to identify your postback in the code behind file. The second argument the doPostback method becomes helpful here.

Using the hidden variables you can also find the ID of the control which causes the postback.  All you need to do is to retrieve the value of the __EVENTTARGET from the form parameter collection.  Take a look at the code below.


If Page.IsPostBack Then
   Dim eventArg As String = Request("__EVENTARGUMENT")
   If eventArg = "MyCustomArgument" Then
       Response.Write("You got it !")
   End If
End If


Did you notice how we identify if the page is loaded as part of our postback? We used the second argument in the __doPostBack method to pass a value and used that in PageLoad to identify if it is called as a result of our PostBack.

By Web Star   Popularity  (9635 Views)
Biography - Web Star
Visit my Blog Web developer using,,Sql Server, Silverlight, Javascript, CSS, AJAX etc.