Using the hidden variables you can also find the ID of the
control which causes the postback. All you need to do is to retrieve the value
of the __EVENTTARGET from the form parameter collection. Take a look at the
code below.
Listing 2 – Getting the _EVENTTARGET hidden field
protected void Page_Load(object sender, EventArgs e)
{
string controlName = Request.Params.Get("__EVENTTARGET");
}
Analysis
For this code to work you need to add any web server control
on the form except for Button and ImageButton control (I will tell you why
later in this article). Let us add the DropDownList control and set the
AutoPostBack property to true and populate the DropDownList with some dummy
data. Now, run the page and view the source of the page.
You will see the following line of code.
Listing 3 – DropDownList calling __doPostBack
function
<select name="DropDownList1"
onchange="javascript:setTimeout('__doPostBack(\'DropDownList1\',\'\')', 0)"
id="DropDownList1">
<option selected="selected" value="One">One</option>
<option value="Two">Two</option>
</select>
The onchange event of the DropDownList calls the
__doPostBack function. The ID of the control, “DropDownList1,” is also passed
to the _doPostBack function and stored in the _EVENTTARGET hidden field. In
the Page_Load I fetch the value of the _EVENTTARGET variable which in this case
is the ID of the DropDownList. This way we can find out that which control
caused the postback.