-1

ここでは、ボタンが 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エンジンがボタンのイベントハンドラーサーバー側を呼び出す方法について非常に簡単な質問があります。

Googleで少し検索した後、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 に答える