C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




How to call Postback from Javascript


Posted Date: 19 Apr 2006    Resource Type: Articles    Category: Web Applications

Posted By: Tony John       Member Level: Gold
Rating:     Points: 20



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.




Responses

Author: Dean McCarthy    17 Jul 2008Member Level: Bronze   Points : 1
Fantastic article, thanks for taking your time to explain everything fully.



Author: @@@ Hyderabadi Biryani @@@    30 Jul 2008Member Level: Diamond   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 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



Feedbacks      
Popular Tags   What are tags ?   Search Tags  
How to Call Postback from Javascript  .  How to Call Postback from Client  .  Client handling  .  Calling Postback from Javascript  .  Calling Postback from Client Side  .  Calling Postback from Client  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Difference between web site and web application in Visual Studio 2005
Previous Resource: Form Authentication – ASP.NET
Return to Discussion Resource Index
Post New Resource
Category: Web Applications


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

conference calls

Contact Us    Privacy Policy    Terms Of Use