【发布时间】:2011-04-19 00:35:22
【问题描述】:
我试图弄清楚点击了哪个按钮,这段代码在 IE 中运行良好,但如果我使用 Chrome、Firefox 或 Safari,它什么也做不了。在 firefox 中使用 firebug 时,我查看了 Form Details,它显示 EVENTTARGET 没有任何值,它只是空白。我怎样才能让它在 FF、Chrome 和 Safari 上运行?
方法:
Control postbackControlInstance = null;
string postbackControlName = page.Request.Params.Get("__EVENTTARGET");
if (postbackControlName != null && postbackControlName != string.Empty)
{
postbackControlInstance = page.FindControl(postbackControlName);
}
else
{
for (int i = 0; i < page.Request.Form.Keys.Count; i++)
{
postbackControlInstance = page.FindControl(page.Request.Form.Keys[i]);
if (postbackControlInstance is System.Web.UI.WebControls.Button)
{
return postbackControlInstance;
}
}
}
if (postbackControlInstance == null)
{
for (int i = 0; i < page.Request.Form.Count; i++)
{
if ((page.Request.Form.Keys[i].EndsWith(".x")) || (page.Request.Form.Keys[i].EndsWith(".y")))
{
postbackControlInstance = page.FindControl(page.Request.Form.Keys[i].Substring(0, page.Request.Form.Keys[i].Length - 2));
return postbackControlInstance;
}
}
}
return postbackControlInstance;
代码调用方法:
if (Page.IsPostBack)
{
try
{
Control cause = GetPostBackControl(Page);
string statecause = cause.ID;
if (statecause == "buttonName1")
{
search(statecause);
}
else if (statecause == "buttonNAME2")
{
resetfields();
}
}
catch { }
}
【问题讨论】: