How to call Postback from Javascript


This article explains how you can explicitly call ASP.NET postback from javascript.

Introduction



Postback is a concept introduced in ASP.NET and is a very handy method. Postback is built into the ASP.NET and most of the web controls support it without writing any code.

Calling postback event from Javascript



There may be some scenario where you may want to explicitly postback to the server using some clientside javascript. It is pretty simple to do this.

ASP.NET already creates a client side javascript method as shown below to support Postbacks for the web controls:


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



So, 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');
</script>



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 %>
</script>



At runtime, it will be evaluated as:


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



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.

How to identify and 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.

Go to the code behind file and write the following code in the Page Load event:


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.


Comments

Author: Dean McCarthy17 Jul 2008 Member Level: Bronze   Points : 1

Fantastic article, thanks for taking your time to explain everything fully.

Author: www.DotNetVJ.com30 Jul 2008 Member Level: Gold   Points : 1

Hi Tony,
Thanks very much for this article, i was able to full-fill my clients requirement on doing postback in javascript.

This article rocks...

Thanks -- Vj
http://dotnetvj.blogspot.com

Author: Gaurav Aroraa03 Aug 2008 Member Level: Gold   Points : 1

Hi Tony!

This is a most valuable for me, I am looking for the same from last few days, but unable to find a perfect way.

Thanks!
Gaurav Arora

Author: Nisar11 Apr 2009 Member Level: Gold   Points : 0

Nice article

Author: Manoranjan Sahoo01 Jun 2009 Member Level: Gold   Points : 0

Very good articles.
Really i need this.

Author: Abhay Kumar Raut02 Jun 2009 Member Level: Gold   Points : 1

Hi Tony!

This is a nice article and the way you present this it is more great than this article.

Thanks

Author: Christopher F06 Oct 2010 Member Level: Gold   Points : 1

Hi Tony,


Good Article,Very nice Explanations

thanks
Christopher F

Author: Er. Ram Singh06 Oct 2010 Member Level: Gold   Points : 1

Author: Mayur13 Oct 2010 Member Level: Gold   Points : 0

Nice Posting Here..

Good
keep it up

Author: Amit Shah14 Oct 2010 Member Level: Gold   Points : 0

Hello,

Good one!

Thanks
Amit Shah

Author: Muhammad Umar21 Mar 2011 Member Level: Bronze   Points : 1

Nice article by Tony John
It was very helpful to me...........


For C# How to identify and handle the postback in code behind ?

if (Request.Params.Get("__EVENTARGUMENT") == "MyCustomArgument")
{}

Author: Mukta Aggarwal05 Apr 2011 Member Level: Silver   Points : 0

THis is really nice one .
Thanks for this userful code.

Author: velmurugan06 Apr 2011 Member Level: Silver   Points : 1

hi tony,

The way you explained the things is excellent. Anybody can understand such kind article without confusion.
Well done.

Regards,
Velmurugan.P

Guest Author: Mike27 Mar 2012

This page is stealing your content:
http://www.aspmemo.net/2011/12/calling-postback-event-from-javascript.html
They have intentionally changed some words and rearranged the paragraphs, but it is still obviously the same article.

Author: Tony John31 Mar 2012 Member Level: Gold   Points : 0

Mike,

Thanks for reporting the issue. We have taken necessary steps.

Guest Author: Shriyal Padte02 May 2012

Thanks a lot. Appreciate your effort.

Guest Author: Bijal21 Nov 2012

I tried the above code in my projects and works fine except in Safari.

I need to do postback when user close the browser. In most of the browsers this is working but not in Safari.

Any suggestions? It's urgent

Guest Author: stan21 Dec 2012

good job my man.
thanks a lot.

Guest Author: Resham16 Apr 2013

It's very helpful for me.. Thanks a lot.

Guest Author: Botenx28 May 2013

You can't run an ASP.NET application from a CD. ASP.NET rqeriues a running instance of IIS, which rqeriues Windows 2000 or later, which won't fit on a CD, nor provide you with the writable directories required by IIS.

Guest Author: Neetish Neerala20 Jun 2013

I tried the above code in my projects and works fine except in Safari.
I need to do postback when user close the browser. In most of the browsers this is working but not in Safari.

Guest Author: Steve14 Feb 2014

Hi,
I have a form (FormA) with a button on it and a label. When the user clicks this button it brings up a popup where the user can update some part costs (FormB). I want the updated part costs from FormB to appear on FormA.Label. Will this method work in this scenario? The label name on FormA is lbAccumRepairCost1



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: