我想弄明白什麼按鈕被點擊了,這段代碼在IE中工作得很好,但是如果我的Chrome,Firefox或Safari沒有做任何事情。在firefox中使用firebug時,我查看了Form Details,它表明EVENTTARGET沒有任何價值,只是空白。我怎樣才能讓它在FF,Chrome和Safari上運行?EVENTTARGET確定發件人的問題
方法:
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 { }
}