How to get the information about the control which caused post back in ASP.NET


In this article, I am going to explore – "How to get the information about the control which caused post back in ASP.NET". For this purpose, I am going to access "__EVENTTARGET" element. I will also explain "__EVENTTARGET" in detail.

In this article, I am going to use "__EVENTTARGET" element.

What is __EVENTTARGET?



If you ever see HTML source code of an ASP.NET page carefully, you would have noticed a hidden input tag added to the form after a postback (see the picture below).

When you postback a form or when AutoPostBack is enabled, .Net framework injects two additional elements to the HTML code:


<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value=""/>


There is also a javascript method named __doPostBack(eventTarget, eventArgument) as:

function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}




The Definition :



The __EVENTTARGET is a hidden variable that tells the server, which control is actually responsible for event which caused a postback, so that the .Net framework can fire the server side event for that control.

We are going to access this hidden input element “__EVENTTARGET” to accomplish our goal. This element can be found in the Form Collections.
Here, we need to take care about one thing. If the postback is done by a Button control, then the page actually gets submitted and no postback occurs. The reason is, a button is rendered as <input type=”submit”> tag and is get added to the Form collection of the page.

Here is the complete code snippet. The code is very simple and can be easily understood:


Control wbControl = GetControl(sender as Page);

private Control GetControl(Page page)
{
Control postBackedControl = null;
string controlName = Page.Request.Params.Get("__EVENTTARGET");

if (controlName != null && controlName != String.Empty)
{
postBackedControl = page.FindControl(controlName);
}
else
{
foreach (string pageControl in page.Request.Form)
{
Control cntrl = Page.FindControl(pageControl);

if (cntrl is System.Web.UI.WebControls.Button)
{
postBackedControl = cntrl;
break;
}
}
}

return postBackedControl;
}


Now, our "wbControl" object is the control which actually caused the postback. You can get other information about the control by accessing its attributes like wbControl.ID etc.

Hope This article is helpful for you. You can also read my first article which describes Different types of Classes in C# .Net - With Definition and Examples

All the best…!


Comments

Guest Author: Rey11 Jun 2012

I am more of a front end developer with itamreedinte experience on the back end, so please forgive my inability to answer these questions on my own:Could you provide an example or two where this would be useful? Maybe you could discuss the pros and cons of this as opposed to web services or update panels. Would this work with forms? thanks

Author: Ajatshatru Upadhyay19 Jun 2012 Member Level: Gold   Points : 2

Dear Rey,

UpdatePanel is used for partial post back of the page and the article is about "determining the control which caused post back".
There are many controls that fires server side event. The article is all about getting the information of that control which actually fired server side event.
As far as web services are concerned,in general terms, they are used for sharing information out side the scope of the application.
Thank you

Author: Fajar22 Jul 2012 Member Level: Bronze   Points : 0

thank



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: