I'm writing some code which mimics the effect of making a postback to a page by executing exactly the same web request that would be generated on clicking a button that triggers the page postback.
The problem is that the response from the web request is not the same as what I get when clicking on the button.
On investigating, I see that even though the Page_Load event is triggered and handled when I execute the web request, the handler for the button click is not being executed (meaning that either the event is not triggered, or it's being triggered but not handled - I'm guessing it's more likely the former case).
So my question is - how does ASP.NET know what button has been clicked so that it can invoke the appropriate handler?
I thought that this was done by using the __EVENTTARGET param - I have correctly set this in the post body of the web request, but this made no difference.
I looked at the decoded __VIEWSTATE argument, but I couldn't see anything obvious in there.
Can anyone provide any further help?
EDIT: Just to be clear, I am not asking how to add a click handler to a web application.
Rather, I am looking at an application that already has a button click event handler, and I want to know how asp.net figures out from an incoming web request what button click event handler code to invoke.
All controls accept Button and ImageButton use JavaScript for causing a postback. To enable postback on these controls one has to set AutoPostBack property to true. When you set this property to true, __doPostBack function is called on event which causes a postback.
PostBack is the name given to the process of submitting all the information that the user is currently working on and send it all back to the server. Postback is actually sending all the information from client to web server, then web server process all those contents and returns back to client.
A refresh mean a complete reload of the page, without any form data. This is essentially an HTTP GET . A post back is when the page is posted to itself (through the form action="" ).
Firstly, an ASP.NET Button <asp:button>
renders as an <input type="submit" runat="server"...>
element on the form. With reference to postbacks, this button behaves differently as compared to other controls.
When clicked <input type="submit"...>
will automatically fire a postback without the help of ASP.NET (try it in an .htm). You can determine which button was clicked by viewing the parameters of the POST body. The button name and its value (Text prop.) will be sent as parameters. __EVENTTARGET is not in the picture and should be null. Also, there will be ONLY ONE button control in the list of parameters according to the specification (the one that was clicked). So now you know which button was clicked.
Then what is the use of __EVENTTARGET?
Other controls, like <asp:DropDownList>
which render as SELECT
, do not fire a postback when clicked, but simply raise an onChange javascript event. If you want to handle the event on the server side via the SelectedIndexChanged
event, ASP.NET will have to do some extra work for you. If you set AutoPostBack = True for the DropDownList, ASP.NET will capture the original onChange JS event and hookup a custom __doPostBack JS function that is responsible for populating the __EVENTTARGET and explicitly submitting the form. You can now determine the control that triggered the the postback by examining __EVENTTARGET.
Hope this helps a bit !
I can't answer your question, but I can tell you how to find out.
Add a line to your page_load: Request.SaveAs(@"c:\temp\request.txt", true); (for an example).
Then click your button to effect the postback.
Then read the contents of request.txt. Is there anything useful or interesting in there?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With