ASP.NET如何提取导致回发的提交按钮名称以及如何引发其事件?

时间:2012-05-18 08:50:47

标签: asp.net webforms

这里我给的渲染html代码只有一个按钮。

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>

</title></head>
<body>
<form method="post" action="WebForm1.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"  value="/wEPDwUKMjA0OTM4MTAwNGRkfc0B7nRWOSrJt3Z50Lk+r5MmkK9k8GG8PK4FAT3XHhM=" />
 </div>

 <div class="aspNetHidden">

<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAgLVlqviBgKM54rGBsRtS0Jrc+rNk0+mSfAVoJasek0SuUeZnx6RZWwMf1mq" />
 </div>
 <div>
    <input type="submit" name="Button1" value="Button" id="Button1" />
 </div>
 </form>
 </body>
 </html>

html非常简单。当我们点击提交按钮时,然后调用提交按钮的关联服务器端事件处理程序。

所以我有一个非常简单的问题,即asp.net引擎如何提取导致回发的按钮名称以及asp.net引擎如何调用按钮的事件处理程序服务器端。

搜索谷歌后,我发现asp.net引擎获取的按钮名称会导致__VIEWSTATE&amp;回复。 __EVENTVALIDATION隐藏字段。这是真的吗?如果是,则asp.net引擎如何从__EVENTVALIDATION中提取按钮名称。请讨论这个asp.net内部问题。

1 个答案:

答案 0 :(得分:1)

这是你的解决方案::

  

HTML ::

<asp:Button ID="Button1" runat="server" Text="click"/><br /><br />
<asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton><br /><br />
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" /><br /><br />
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
    <asp:ListItem>--Select--</asp:ListItem>
    <asp:ListItem>a</asp:ListItem>
    <asp:ListItem>b</asp:ListItem>
    <asp:ListItem>c</asp:ListItem>
    <asp:ListItem>d</asp:ListItem>
</asp:DropDownList><br /><br />
PostBack Control :: <asp:Label ID="Label3" runat="server" Text="None"></asp:Label>
  

Code Behind ::

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
        Label3.Text = getPostBackControlName();
}

private string getPostBackControlName()
{
    Control control = null;

    //first we will check the "__EVENTTARGET" because if post back made by the controls
    //which used "_doPostBack" function also available in Request.Form collection.

    string ctrlname = Page.Request.Params["__EVENTTARGET"];
    if (ctrlname != null && ctrlname != String.Empty)
    {
        control = Page.FindControl(ctrlname);
    }
    // if __EVENTTARGET is null, the control is a button type and we need to
    // iterate over the form collection to find it

    else
    {
        string ctrlStr = String.Empty;
        Control c = null;
        foreach (string ctl in Page.Request.Form)
        {
            //handle ImageButton they having an additional "quasi-property" in their Id which identifies

            //mouse x and y coordinates

            if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
            {
                ctrlStr = ctl.Substring(0, ctl.Length - 2);
                c = Page.FindControl(ctrlStr);
            }

            else
            {
                c = Page.FindControl(ctl);
            }

            if (c is System.Web.UI.WebControls.Button || c is System.Web.UI.WebControls.ImageButton)
            {
                control = c;
                break;
            }
        }
    }
    return control.ClientID;
}