我在aspx页面上有一个按钮
<asp:Button runat="server" CssClass="sc-ButtonHeightWidth" ID="btnFirstSave" Text="Save" OnClick="btnSave_Click" />
Run Code Online (Sandbox Code Playgroud)
我试图在后面的代码中获取事件目标和事件源,以便基于它进行一些验证.我试过下面的代码.
string ctrlname = page.Request.Params.Get("__EVENTTARGET");
string ctrlname = Request.Form["__EVENTTARGET"];
string ctrlname = Request.Params["__EVENTTARGET"];
Run Code Online (Sandbox Code Playgroud)
但以上所有都给了我空洞的价值观.如何获得每次都引起回发的控件.我上面做错了什么?
仅供参考:我已经尝试过本链接中提到的解决方案.但它唯一的返回按钮文本给我.我想要buttonID.
Sur*_*ngh 13
Asp按钮渲染为input类型,submit此方法不会_EVENTTARGET
使用"__doPostBack"方法填充控件,导致回发将添加值,_EVENTTARGET
因此您的按钮ID丢失,_EVENTTARGET您可以遍历页面中的所有控件,以检查哪个控件导致回发.
试试这个来捕捉你的控制 - 在这里
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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30243 次 |
| 最近记录: |