Resources » .NET programming » ASP.NET/Web Applications

How to call Postback from Javascript


Last Updated:   Category: ASP.NET/Web Applications    
Author: Member Level: Gold    Points: 20


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.


Did you like this resource? Share it with your friends and show your love!

Responses to "How to call Postback from Javascript"
Author: Dean McCarthy    17 Jul 2008Member Level: Bronze   Points : 1
Fantastic article, thanks for taking your time to explain everything fully.



Author: www.DotNetVJ.com    30 Jul 2008Member 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 Kumar Arora    03 Aug 2008Member 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: Nisar    11 Apr 2009Member Level: Gold   Points : 0
Nice article


Author: Manoranjan Sahoo    01 Jun 2009Member Level: Gold   Points : 0
Very good articles.
Really i need this.



Author: Abhay Kumar Raut    02 Jun 2009Member 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: Domenic    04 Aug 2010Member Level: Bronze   Points : 2
Our system is mostly PHP and we currently have a page called email_notification.php.

All this page does is pull the URL variables when it is hit and creates an email and sends it out. We now have a customer who needs this on an ASP server, the folks who host the site will not turn on PHP.

We do call tracking, so when a call is hung up our customers have the ability on the back end to set their post back page. They use this email_notification page if they want an email sent to them letting them know a call happend and also the variables: phone_nuimber, call_duration, call_date, link, limit (if call duration is equal to or greater than limit then send email) and email (the email address they specify in our system to send the email to that always gets passed in the URL)

I am really new at all of this and really do not know how to approach this. Can someone please help or point me to some good examples?



Author: Christopher F    06 Oct 2010Member Level: Gold   Points : 1
Hi Tony,


Good Article,Very nice Explanations

thanks
Christopher F



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



Author: Mayur    13 Oct 2010Member Level: Gold   Points : 0
Nice Posting Here..

Good
keep it up



Author: Amit Shah    14 Oct 2010Member Level: Gold   Points : 0
Hello,

Good one!

Thanks
Amit Shah



Author: Muhammad Umar    21 Mar 2011Member 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 Aggarwal    05 Apr 2011Member Level: Silver   Points : 0
THis is really nice one .
Thanks for this userful code.



Author: velmurugan    06 Apr 2011Member 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: Mike     27 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 John    31 Mar 2012Member Level: Gold   Points : 0
Mike,

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



Guest Author: Shriyal Padte     02 May 2012
Thanks a lot. Appreciate your effort.


Guest Author: Bijal     21 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: stan     21 Dec 2012
good job my man.
thanks a lot.



Guest Author: Resham     16 Apr 2013
It's very helpful for me.. Thanks a lot.


Guest Author: Botenx     28 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 Neerala     20 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: Steve     14 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



Feedbacks      

Post Comment:




  • 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:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India