Reputation: 3059
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.
Upvotes: 5
Views: 6971
Reputation: 6952
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 !
Upvotes: 7
Reputation: 7592
Shoko,
ASP.net postbacks can be tricky, but you should take a look at this article using the __doPostBack() javascript function for some insight.
http://www.dotnetspider.com/resources/1521-How-call-Postback-from-Javascript.aspx
HTH,
Mike
Upvotes: 0
Reputation: 3059
OK, so I figured this out in the end.
The request should contain a name value pair, where the name is the id of the clicked button, and the value can be '1'.
What puzzled me is that in the request generated by clicking the button, I did not see such a name value pair. Very strange indeed.
Nevertheless, adding this name value pair to my created web request caused the button click event handler code to be fired.
Upvotes: 0
Reputation: 6867
It looks like really only the __EVENTTARGET value needs to be posted with the ID of the button you want to click. You have to set EnableEventValidation="false" or ASP.NET will throw an error. Check out this example. Plain html page, posts to asp.net page, simulating a button click on that page, which should be similar to what you are trying to do.
HTML page:
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<form action="Default.aspx" method="post">
<input value="cmdSubmit" name="__EVENTTARGET" type="hidden" />
<input type="submit" value="submit" />
</form>
</body>
</html>
ASP.NET Page with Button to Click (Default.aspx):
<%@ Page Language="C#" EnableEventValidation="false" %>
<script runat="server">
protected void cmdSubmit_Click(object sender, EventArgs e)
{
litResult.Text = "Submit button clicked";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<form id="form1" runat="server">
<asp:Literal ID="litResult" runat="server" EnableViewState="false"></asp:Literal>
<asp:Button UseSubmitBehavior="false" ID="cmdSubmit" EnableViewState="false" runat="server"
OnClick="cmdSubmit_Click" Text="Submit" />
</form>
</body>
</html>
Upvotes: 0
Reputation: 255
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?
Upvotes: 2
Reputation: 7201
There are a number of easy ways to create an event handler for a button. In layout mode, double-click on the button. Or, also in layout mode, select the button, open the Properties window, click the "lightning bolt" icon, and see a list of events the control can trigger. Double-click on the event you want.
When you do one of these things, Visual Studio, will create the event handler in your code-behind, and wire up the event.
The button event handler runs after page load, and after page validation if you're using ASP.NET validators.
Upvotes: 0
Reputation: 47387
In your code behind, you have to have an event to process, and that event has to have Handles ButtonName.Click
IE:
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
''# Process everything you want to process when Button2 gets clicked
End Sub
Alternatively, in your button you can have OnClick
set in the markup.
<asp:button ID="Button2" runat="server" OnClick="Button2_Click" Text="Click Me" />
Because the button is set to runat="server"
, .NET will do all the heavy lifting for you.
Upvotes: 0