哪个控件导致了后台回发?

44

我有两个按钮:

<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:Button ID="Button2" runat="server" Text="Button" />

在页面加载时,我如何确定这两个控件中哪个导致了postback?是否有简短的解决方案,因为我知道只有两个控件会导致此postback?


3
http://aspnetnova.blogspot.com/2009/04/find-post-back-control-in-aspnet-page-c.html - Sarawut Positwinyu
5个回答

69
您可以使用这种方法来获取引起PostBack的控件:
/// <summary>
/// Retrieves the control that caused the postback.
/// </summary>
/// <param name="page"></param>
/// <returns></returns>
private Control GetControlThatCausedPostBack(Page page)
{
    //initialize a control and set it to null
    Control ctrl = null;

    //get the event target name and find the control
    string ctrlName = page.Request.Params.Get("__EVENTTARGET");
    if (!String.IsNullOrEmpty(ctrlName))
        ctrl = page.FindControl(ctrlName);

    //return the control to the calling method
    return ctrl;
}

这不是在一个重复器中查找控件,因为控件名称为空。有什么想法如何找到它吗? - djmj
第一个 Page.Request 中的 "Page" 应该翻译为 page。 - LarryBud
7
在许多情况下会返回null。正确的解决方法是:https://dev59.com/kHA75IYBdhLWcg3wqK7_ - Tillito
3
当OP问到一个按钮如何工作时,__EVENTTARGET在按钮引起的回发时只是一个空字符串。 - sab669
1
请注意,page.Request.Params.Get("__EVENTTARGET") 返回唯一标识符。 - Gqqnbig
有没有办法获取按钮控件?它在提交按钮上不起作用... - Alessandro

13

点击此处

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

        }

        if (control != null)
            return control.ID;
        else
            return string.Empty;
    }

1
这个版本已经更新至2016年7月22日。 - mbomb007

3
这有助于在页面加载时找到导致postback的控件名称。这对我很有帮助,希望也能帮到其他人。
  <asp:Button ID="Button1" runat="server" Text="Button"
  OnClientClick = "SetSource(this.id)" />

   <asp:ImageButton ID="ImageButton1" runat="server"
    OnClientClick = "SetSource(this.id)" />

            <script type = "text/javascript">
            function SetSource(SourceID)
          {
    var hidSourceID =
    document.getElementById("<%=hidSourceID.ClientID%>");
    hidSourceID.value = SourceID;
    }
    </script>

 on code behind you can get the ID of the function using :
 if (IsPostBack)
 {
   string CtrlID = string.Empty;
  if (Request.Form[hidSourceID.UniqueID] != null &&
       Request.Form[hidSourceID.UniqueID] != string.Empty)
    {
     CtrlID = Request.Form[hidSourceID.UniqueID];
   }
}

2

补充一下:你可以通过以下代码找到导致postback的事件:

Page.Request.Params.Get("__EVENTARGUMENT")

-1
protected void ReUsedMethod_Click(object sender, EventArgs e)
{
  string ctrlName = ((Button)sender).ID;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接