2012-05-18 26 views
-1

這裏我給我的渲染HTML代碼只有一個按鈕。ASP.NET如何提取導致回發的提交按鈕名稱以及如何提升其事件?

<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內部問題。

+0

輸入控件的ID是postdata。 – Aristos

回答

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; 
} 
+0

這對你有用嗎? –

+0

我會檢查並回復你。你可以解釋什麼是__EVENTVALIDATION隱藏字段的用法。什麼樣的數據被存儲到__EVENTVALIDATION隱藏字段中。 – Thomas

相關問題