How does the asp.net page know which button caused the postback?

I am writing code that mimics the effect of sending to a page, executing exactly the same web request that will be generated when a button is clicked, which starts the postback of the page.

The problem is that the response from the web request does not match what I get when I click on the button.

When checking, I see that even when the Page_Load event is triggered and processed when the web request is executed, the handler for clicking the button is not executed (this means that either the event is not triggered or it is triggered but not processed - I assume that this is probably the first happening).

So my question is , how does ASP.NET know which button was pressed so that it could invoke the appropriate handler?

I thought this was done using the __EVENTTARGET parameter - I set it correctly in the body of the web request email, but it didn’t matter.

I looked at the decoded __VIEWSTATE argument, but I could not see anything obvious there.

Can anyone help?

EDIT: To be clear, I am not asking how to add a click handler to a web application.

Rather, I'm looking at an application that already has a button click event handler running, and I want to know how asp.net calculates from an incoming web request which button code intercepts the call event handler code.

+5
source share
7 answers

OK, so I figured it out at the end.

, clicked, "1".

, , , ​​ . .

, - .

0

-, ASP.NET <asp:button> <input type="submit" runat="server"...> . , - .

<input type="submit"...> ASP.NET ( .htm). , , POST. () . __EVENTTARGET . , (, ) ONE. , , .

__EVENTTARGET?

, <asp:DropDownList>, SELECT, , onChange javascript. SelectedIndexChanged, ASP.NET . AutoPostBack = True DropDownList, ASP.NET onChange JS __doPostBack JS, __EVENTTARGET . , , __EVENTTARGET.

, !

+5

, , .

page_load: Request.SaveAs(@ "c:\temp\request.txt", true); ( ).

, .

request.txt. - ?

+1

, 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

, OnClick .

<asp:button ID="Button2" runat="server" OnClick="Button2_Click" Text="Click Me" />

​​ runat="server",.NET .

0

. . , , , "", "" , . .

, Visual Studio .

, ASP.NET.

0

, __ EVENTTARGET , . EnableEventValidation = "false" , ASP.NET . . Plain html page, asp.net, , , .

HTML-:

<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 (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>
0

,

Postbacks ASP.net , , javascript __doPostBack() .

http://www.dotnetspider.com/resources/1521-How-call-Postback-from-Javascript.aspx

,

Mike

0
source

All Articles