Get control name in Page_Load event after post back by any control
Add script
--------------------
function __doPostBack(eventTarget, eventArgument)
{
if (!theForm.onsubmit || (theForm.onsubmit() ! = false ))
{
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument; theForm.submit();
}
}
----------------------
<input type= "hidden" name= "__EVENTTARGET" id= "__EVENTTARGET" value= "" />
<input type= "hidden" name= "__EVENTARGUMENT" id= "__EVENTARGUMENT" value= "" />
<select name="DropDownList1" onchange="javascript:setTimeout('__doPostBack(\'DropDownList1\',\'\')', 0)" id="DropDownList1">
<option value="1">abc</option>
<option value="2">xyz</option>
</select>
---------------------
private string getPostBackControlName()
{
Control control = null;
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;
}
}
}
string id = "";
if ( control != null) id=control.ID;
return id;
if ( control != null) id=control.ID;
return id;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
Response.Write(getPostBackControlName());
}
--------------------
Comments
Post a Comment