Asp.net 如何获取回发邮件的来源
如果(第IsPostBack页) { //这里我需要知道哪个控件导致回发 } 谢谢查看此帖子Asp.net 如何获取回发邮件的来源,asp.net,postback,Asp.net,Postback,如果(第IsPostBack页) { //这里我需要知道哪个控件导致回发 } 谢谢查看此帖子 以下是“标记为答案”链接中的代码(只需将代码粘贴在此处,以便节省读者时间): 通常最好包含主代码示例(就像编辑之前一样),以防您链接到的页面被删除,当然也要保留到文章的链接:) private string getPostBackControlName() { Control control = null; //first we will check the "__EVENTTARG
以下是“标记为答案”链接中的代码(只需将代码粘贴在此处,以便节省读者时间):
通常最好包含主代码示例(就像编辑之前一样),以防您链接到的页面被删除,当然也要保留到文章的链接:)
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.AllKeys)
{
c = Page.FindControl(ctl);
if (c is System.Web.UI.WebControls.Button ||
c is System.Web.UI.WebControls.ImageButton )
{
control = c;
break;
}
}
}
if (control == null)
return "";
else
return control.ID;
}