In this article, we will take a closer look at how ASP.NET pages post back to themselves, and how to customize this feature in our web applications.
function __doPostBack(eventTarget, eventArgument)
One of the most important features of the ASP.NET environment is the ability to declare controls that run on the server, and post back to the same page. Remember the days of classic ASP? We would create a form which would accept the user’s input, and then we would most probably have to create another page that would accept all those inputs, either through HTTP GET or POST, and perform some kind of validation, display and action. Sometimes, even a third page was necessary to perform our actions. This wasted a lot of time and complicated things when you had to make a change. But of course, this is not necessary any more with ASP.NET. There is no need to create second pages that accept the inputs of the first, process them and so on. Form fields and other controls can be declared to run on the server, and the server simply posts the page back to itself and performs all the validation, display and actions. Our life as web developers has become a million times better. But how exactly is this done?
When a control is declared to run on the server, a VIEWSTATE is created which remembers the ID of that control, and the method to call when an action is performed. For example, let’s say we run the following code:
<script language="VB" runat="server"> Sub Test_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 'enter your code to perform End Sub </script> <html> <body> <form runat="server" id="myForm"> <asp:linkbutton id="Test" runat="server" text="Create Text file" onclick="Test_Click" /> </form> </body> </html>
This is a very simple page. We declare only one web control, a linkbutton, to run on the server, with an ID of Test and we assign a method called Test_Click to run when the link is clicked on the page. The linkbutton has to be wrapped inside a form that runs on the server as well. We save the above as an ASPX page and then we browse to it. The HTML that gets created looks like this:
- eventTarget: the control doing the submission
- eventArgument: any additional information for the event
... <asp:linkbutton id="Test" runat="server" text="Create Text file" onclick="Test_Click" visible="false" /> ...
So now that we have seen how the postback process works, we can easily create web applications to use this feature. Let’s go through an example, and hopefully things will clear up more.
Let’s see the HTML code needed to do this:
Inside our form we have 3 controls:
- a linkbutton that runs on the server, with ID CreateFile, and a CreateFile_Click method which runs when it is clicked.
Since the linkbutton has an event called CreateFile_Click which runs on the server when it is clicked, then the page will submit to itself, and this method will run. The first thing we want to do in this method is get the name of the filename, and this is done by funcParam.Value. The remaining code to create the actual file on the server is not included, since the purpose of this article is not to show this. You can add it in, or create some other code here that performs a different action.
We have seen how the postback function works with ASP.NET, and how to use it in our web applications. This article has concentrated on how to use this with a simple prompt box, but you can use a similar technique to combine the __doPostBack function with a showModalDialog function, or to return a value from another pop up window.
*I don’t think it is an appropriate sample code for __doPostback() scenario. Even if you comment out __doPostBack(
'CreateFile',''); it works unchanged!
Clean your browser cache!
Very good explanation. Concept is clear now. Thanks a lot.
This is a very informative article. Thanks for sharing your knowledge. I have findout few other links that also descrebed in a good way about (PostBack,_doPostBack
Thank you for this excellent explanation!!!!
Is Postback is normally used on page _load event to detect if the web page is getting generated due to postback requested by a control on the page or if the page is getting loaded for the first time.
http://net-informations.com/faq/asp/ispostback.htm asp.net ispostback()
You might have already solved this, but make sure you copy the example code as is. Since the client and server side function names are similar in my example one can easily confuse the two.
A valuable lesson I learned today. I visited your site. Thanks for posting this informative and easy to read guide to the postback.
I am working on a “Web Shop Cart” using .NET VB (jquery, jscript)
The shop items are generated at run time and With the selection
an animated image to be inserted in cart.
Having problem with the cart, can you give help.