I am trying to figure out what button was clicked, this code works just fine in IE, but if I Chrome, Firefox, or Safari it doesnt do anything. When using firebug in firefox, I looked at the Form Details, and it shows that EVENTTARGET has no value its just blank. How can I get this to work on FF, Chrome, and Safari?
Method:
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;
Code Calling Method:
if (Page.IsPostBack)
{
try
{
Control cause = GetPostBackControl(Page);
string statecause = cause.ID;
if (statecause == "buttonName1")
{
search(statecause);
}
else if (statecause == "buttonNAME2")
{
resetfields();
}
}
catch { }
}
The best way is to determine what control that caused the postback is to override
protected
Page.RaisePostBackEvent
method. This method is used by ASP.NET infrastucture to notify the server control that caused the postback that it should handle an incoming postback event:The code you provided should work for scenarious when the client-side
__doPostBack
function is rendered to the page (e.g. if you use the only one button like<asp:Button runat="server" ID="btnSubmit" Text="submit" UseSubmitBehavior="true" />
it won't be rendered) only.If even in the case when the
__doPostBack
function is rendered, but__EVENTTARGET
parameter is empty it means that the default behaviour of the__doPostBack
function is violated by custom/incompatible javascript code in the most cases. In this case even ASP.NET infrastructure will be unable to handle post back events properly.