c# – __EVENTTARGET在按钮点击的回发上为空
发布时间:2020-12-15 17:51:14 所属栏目:百科 来源:网络整理
导读:我在aspx页面上有一个按钮 asp:Button runat="server" CssClass="sc-ButtonHeightWidth" ID="btnFirstSave" Text="Save" OnClick="btnSave_Click" / 我正在尝试将事件目标和事件源代码放在后面,以便根据它进行一些验证.我试过下面的代码. string ctrlname =
我在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"]; 但以上所有都给我空值.如何获得每次导致回发的控制.我上面做错了吗? FYI:我已经尝试了这个LINK提到的解决方案.我想要buttonID. 解决方法
Asp按钮渲染为输入类型,提交此方法将不会fill_EVENTTARGET
控制使用“__doPostBack”方法导致回发会将值添加到_EVENTTARGET 所以你的按钮ID从_EVENTTARGET中丢失,你可以遍历页面中的所有控件来检查哪个控件引起回发. 尝试这个捕获你的控制-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; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |