【问题标题】:__EVENTTARGET is empty on postback of button click__EVENTTARGET 在按钮单击回发时为空
【发布时间】:2013-12-30 10:51:04
【问题描述】:

我在 aspx 页面上有一个按钮

<asp:Button runat="server" CssClass="sc-ButtonHeightWidth" ID="btnFirstSave" Text="Save" OnClick="btnSave_Click" />

我正在尝试在后面的代码中获取事件目标和事件源,以基于它进行一些验证。我尝试了以下代码。

string ctrlname = page.Request.Params.Get("__EVENTTARGET");
string ctrlname = Request.Form["__EVENTTARGET"];
string ctrlname = Request.Params["__EVENTTARGET"];

但以上所有内容都给了我空值。如何获得导致每次回发的控制权。我在上面做错了吗?

仅供参考:我已经尝试过LINK 中提到的解决方案。但它只为我返回按钮文本。我想要buttonID。

【问题讨论】:

  • 试试&lt;asp:Button runat="server" usesubmitbehavior="false" ...
  • @Damith 感谢您的评论。将此属性设置为“false”有什么问题吗?我在下面看到 Vignesh Kumar 和 Suraj Singh 的一些答案,这也是一个可行的解决方案。但是与他们相比,您的评论更容易。我经历了“msdn.microsoft.com/en-us/library/…”。但我没有得到任何答案“它会导致按钮的任何行为变化吗?”
  • @Naveen 当我们执行 usesubmitbehavior="false" 时,inputtypesubmit 更改为 button
  • 此更改会产生影响:表单可提交,但使用提交类型禁用了 javascript。但是对于按钮类型,如果没有 javascript,表单将无法提交。 (当然,在您的情况下,这可能不是问题。)

标签: c# asp.net


【解决方案1】:

Asp 按钮呈现为 input 类型为 submit 此方法不会填充_EVENTTARGET 使用"__doPostBack" 方法导致回发的控件将向_EVENTTARGET 添加值 因此_EVENTTARGET 中缺少您的按钮 ID,您可以遍历页面中的所有控件以检查哪个控件导致回发。

试试这个来捕获你的控制 -Here

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.ID;

        }

【讨论】:

  • 感谢 Suraj 的回答。我有一些疑问,我在我的问题下添加了评论。你对此有什么想法吗?
  • @Naveen UseSubmitBehavior 属性指定 Button 控件是使用浏览器的内置提交功能还是 ASP.NET 回发机制。所以我不认为使用Damien 解决方案会导致任何问题。查看按钮何时呈现为&lt;input 提交类型它不使用Asp.Net 回发机制,因此它不会通过更改填充_Eventtarget提交行为你只是确保它使用Asp.Net机制("_doPostback")我认为它比我的解决方案更适合你目前的情况。
  • @Suraj 谢谢。甚至我也这么认为。但你也给出了一个很好的解决方案,对此投赞成票。
  • 我觉得linked here的回答比较全面一点。不过仍然是一个很好的答案。
  • @David Rogers 感谢您的赞赏,我会尽快让这个答案更有效率。
【解决方案2】:

到目前为止,我已将 usesubmitbehavior 设置为 false。 Damith 在上面的评论中给出了一个很好的解决方案。到目前为止,它对我来说工作正常,没有任何问题。了解楼盘Read this LINK

希望这会对某人有所帮助。

【讨论】:

    【解决方案3】:

    只需将 UseSubmitBehavior="False" 添加到您的按钮即可。

    <asp:Button runat="server" CssClass="sc-ButtonHeightWidth" ID="btnFirstSave" Text="Save" OnClick="btnSave_Click" UseSubmitBehavior="False" />
    

    【讨论】:

      【解决方案4】:
      /// <summary>
      /// Gets the ID of the post back control.
      /// 
      /// See: http://geekswithblogs.net/mahesh/archive/2006/06/27/83264.aspx
      /// </summary>
      /// <param name = "page">The page.</param>
      /// <returns></returns>
      public static string GetPostBackControlId(this Page page)
      {
          if (!page.IsPostBack)
              return string.Empty;
      
          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 controlName = page.Request.Params["__EVENTTARGET"];
          if (!String.IsNullOrEmpty(controlName))
          {
              control = page.FindControl(controlName);
          }
          else
          {
              // if __EVENTTARGET is null, the control is a button type and we need to
              // iterate over the form collection to find it
      
              // ReSharper disable TooWideLocalVariableScope
              string controlId;
              Control foundControl;
              // ReSharper restore TooWideLocalVariableScope
      
              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"))
                  {
                      controlId = ctl.Substring(0, ctl.Length - 2);
                      foundControl = page.FindControl(controlId);
                  }
                  else
                  {
                      foundControl = page.FindControl(ctl);
                  }
      
                  if (!(foundControl is Button || foundControl is ImageButton)) continue;
      
                  control = foundControl;
                  break;
              }
          }
      
          return control == null ? String.Empty : control.ID;
      }
      

      Source

      【讨论】:

        【解决方案5】:

        我的解决方案。我在客户端添加这样的 OnClick 事件

        function btnClickSuper(s, e) { __doPostBack(s.name, ''); }

        这里s 是我在DevExpress 中的按钮呈现的对象,其中属性name 包含作为元素ID 的一部分。所以,在服务器端,我可以获得按钮的 ID 发送回传。价格是服务器 OnClick 事件未触发。

        【讨论】:

          猜你喜欢
          • 2011-12-08
          • 1970-01-01
          • 2011-11-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多