-1

在这里,我给出了只有一个按钮的渲染 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 和 __EVENTVALIDATION 隐藏字段获取导致回发的按钮名称。是真的吗?如果是,那么 asp.net 引擎如何从 __EVENTVALIDATION 中提取按钮名称。请讨论这个 asp.net 内部问题。

4

1 回答 1

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>

后面的代码::

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;
}
于 2012-05-18T10:13:14.667 回答