按钮点击回发后__EVENTTARGET为空

我在aspx页面上有一个按钮

 

我试图在后面的代码中获取事件目标和事件源,以便根据它进行一些validation。 我试过下面的代码。

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

但以上所有都给了我空洞的价值观。 如何获得每次都引起回发的控件。 我上面做错了什么?

仅供参考:我已经尝试过本链接中提到的解决方案。 但它唯一的返回按钮文本对我来说。 我想要buttonID。

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

试试这个来捕捉你的控制 – 在这里

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

到目前为止,我已将usesubmitbehavior设置为false。 Damith在评论中给出了一个很好的解决方案。 截至目前,它对我来说没有任何问题。 了解该物业阅读此链接

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

只需在按钮上添加UseSubmitBehavior =“False”即可。

  

 ///  /// Gets the ID of the post back control. /// /// See: http://geekswithblogs.net/mahesh/archive/2006/06/27/83264.aspx ///  /// The page. ///  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; } 

资源

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

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

这里是对象在DevExpress中显示我的按钮,其中属性name包含,作为el的id的一部分。 所以,在服务器端,我可以获得发送回发的按钮的Id。 价格是服务器OnClick enevnt未被触发。