【问题标题】:Which control caused the postback?哪个控件导致了回发?
【发布时间】:2011-09-01 10:51:33
【问题描述】:

我有两个按钮:

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

如何在 pageLoad 上确定这两个中的哪一个导致了回发? 是否有一个简短的解决方案,因为我知道只有两个控件可以导致此回发?

【问题讨论】:

标签: asp.net postback


【解决方案1】:

您可以使用此方法获取导致回发的控件:

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

【讨论】:

  • 如果控件在转发器中,则找不到控件,因为控件名称为空。知道如何找到这个吗?
  • 第一个Page.Request中的“Page”应该是page.Request
  • 在许多情况下返回 null。正确的解决方案是:stackoverflow.com/questions/3175513/…
  • 按照 OP 的要求,这对按钮有何作用? __EVENTTARGET 只是一个空字符串,当回发是由一个按钮引起的时
  • 注意,page.Request.Params.Get("__EVENTTARGET") 返回唯一 ID。
【解决方案2】:

http://geekswithblogs.net/mahesh/archive/2006/06/27/83264.aspx

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

【讨论】:

【解决方案3】:

这有助于找到导致页面加载回发的控件名称。这对我有帮助。希望这对其他人也有帮助

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

【讨论】:

    【解决方案4】:

    补充一点:您可以通过以下方式找到导致回发的事件:

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

    【讨论】:

      【解决方案5】:
      protected void ReUsedMethod_Click(object sender, EventArgs e)
      {
        string ctrlName = ((Button)sender).ID;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-03-11
        • 1970-01-01
        • 2011-12-27
        • 1970-01-01
        • 1970-01-01
        • 2012-05-01
        • 1970-01-01
        相关资源
        最近更新 更多