views:

88

answers:

6

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.

A: 

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.

rockinthesixstring
Hi Rockinthesixstring, as I said to Cylon Cat above, I don't want to handle the click event - I just want to figure out why the existing click event is not firing when I invoke a web request that is identical to the request that is invoked on a button click. If I knew what exactly asp.net was looking for in the web request in order to invoke the click handler code, then I would add this to the web request - hence my original question about how the asp.net parses the web request that is sent to figure out which handler to invoke.
Shoko
A: 

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.

Cylon Cat
Hi Cylon Cat, I **don't** want to create an event handler for a button. Rather, I want to exercise server code in an web application which is supposed to handle a button click. I want to do this by invoking a web request which is identical to the web request that is generated when a button is clicked. I believe that doing so should cause the button click handler code to be called, but this is not happening. So I want to know what asp.net looks for in a web request in order figure out what event handler code should be invoked.
Shoko
Web requests are handled very differently from button clicks, so if you want to treat this as a web request, you can go with two ways. One is by using Page Methods, and the other is by creating a web service. I havent used Page Methods, but they are AJAX-callable methods within a page, and you'll need an asp:ScriptManager tag specifying that you're going to use Page Methods. If you create a web service, the service code will run in-session in ASP.NET, and have access to session variables. You can code a service reference in the asp:ScriptManager; that makes the service callable from JavaScript
Cylon Cat
+1  A: 

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?

wozza
Thanks, Wozza. I'll give it a go and let you know what I find out.
Shoko
A: 

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"&gt;
<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"&gt;
<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>
Chris Mullins
Chris, I might generate the javascript - but how would this answer my question of how asp.net knows how to trigger particular event handling code?
Shoko
It appears to me that the javascript is setting the value of the __EVENTTARGET hidden field with the ID of the control that is being clicked, and then submitting the form. But there is probably more to it than that, like maybe the viewstate field cannot be empty to indicate that it is a postback.
Chris Mullins
That's a good point, Chris, and that's what I thought myself. I checked the __EVENTTARGET field of the request that was generated on clicking the button, and it had the id of the button that was clicked. I then ensured that my request had the same __EVENTTARGET value, but this did not appear to trigger the button click event handler. So maybe there is more to it. I wish someone could shed more light here...
Shoko
I did some testing and updated my answer. I was able to post to the asp.net page from a plain html page and trigger the button click.
Chris Mullins
A: 

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.

Shoko
A: 

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

Mike C