c# – __EVENTTARGET在按钮点击的回发上为空

c# – __EVENTTARGET在按钮点击的回发上为空,第1张

概述我在aspx页面上有一个按钮 <asp:Button runat="server" CssClass="sc-ButtonHeightWidth" ID="btnFirstSave" Text="Save" OnClick="btnSave_Click" /> 我正在尝试将事件目标和事件源代码放在后面,以便根据它进行一些验证.我试过下面的代码. string ctrlname = page.Req 我在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;        }
总结

以上是内存溢出为你收集整理的c# – __EVENTTARGET在按钮点击的回发上为空全部内容,希望文章能够帮你解决c# – __EVENTTARGET在按钮点击的回发上为空所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1237595.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存