Click here to Skip to main content
11,791,059 members (58,283 online)
Click here to Skip to main content

Way To Know Which Control Has Raised PostBack

, 13 Dec 2010 CPOL 82.7K 436 56
Rate this:
Please Sign up or sign in to vote.
This is the short code snippet with understanding to make you know about what is PostBack in ASP.NET and further how you can know about the Control that raised the PostBack by using inbuilt ASP.NET functionality and manually.


The main aim of this article is to make you know among the several controls that resides in ASP.NET page, at the time of PostBack how to know which control has actually raised this event.


Technical definition about PostBack: Most ASP.NET Controls are "Server Controls". This means that when an action occurs, the Page, which hosts all its controls inside <FORM... tags, makes a regular old HTTP POST, with itself being the target URL. This is referred to as a "POSTBACK". *

In simple words, we can differentiate PostBack and Redirection in this way.

Postback: It is the scenario in which ASP.NET page redirects to page itself.

Redirect: It is the scenario in which ASP.NET page is called from another page.

You can check whether the page is PostedBack or not with code side using IsPostBack Property which returns Bool.

Using the Code

The agenda of this article is to determine which control inside page has made page to be posted back to itself OR in other words which control has raised PostBack.

For the control which supports AutoPostBack property, ASP.NET Page has a inbuilt function which will determine the event raising control.

There are several controls that support AutoPostBack, among them some are:

  • Dropdown
  • CheckBoxList
  • RadioButtonList
  • TextBox, etc.

These controls calls _doPostback function in JavaScript generated by ASP.NET automatically.

Let's have a look at the _doPostback function.

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


  • This function will not be visible until minimum of one control is raising the AutoPostBack inside page.
  • The _doPostBack event simply stores value in two hidden fields.
    1. eventTarget - Name of the control raised the postback
    2. eventArgument - The argument to be sent to server

Let's have a look at how those two hidden fields are on page.

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

Now if you want to get the ID of a raising control from the code side, you can write this simple snippet and find the values stored in hidden field:

if (IsPostBack)
    string ControlID = string.Empty;
    if (!String.IsNullOrEmpty(Request.Form["__EVENTTARGET"]))
        ControlID = Request.Form["__EVENTTARGET"];

OR alternatively, you can find full control object using:

if (IsPostBack)
    string ControlID = string.Empty;
    if (!String.IsNullOrEmpty(Request.Form["__EVENTTARGET"]))
        ControlID = Page.Request.Params["__EVENTTARGET"];
        Control postbackControl = Page.FindControl(ControlID);

_doPostBack function only will be called by AutoPostBack property supportive Controls now let's have a look of the other Control which don't support that inbuilt.

Controls that can be listed are:

  • Button
  • ImageButton, etc.

To make a functionality for Non Supportive AutopostBack Property Control, make one Hidden field on the page.

<asp:HiddenField ID="CustomHiddenField" runat="server" />

Call a custom Js function on the clientclick so that it will be stored in hidden field before page actually redirects to server.

<asp:Button ID="btn" runat="server" Text="Click me"
  OnClientClick = "SetSource(" />

function SetSource(SourceID)
        var hidSourceID =
        hidSourceID.value = SourceID;

Note: Always use "<%=CustomHiddenField.ClientID%>" convention because Hiddenfield ID can be renamed by ASP.NET.

And get a name once page is posted back by server using Codebehind.

if (IsPostBack)
    string CtrlID = string.Empty;
    if (!String.IsNullOrEmpty(Request.Form[CustomHiddenField.UniqueID]))
        CtrlID = Request.Form[CustomHiddenField.UniqueID];

Now below is a full code snippet for getting raising control among both type of controls.

protected void Page_Load(object sender, EventArgs e)
    if (IsPostBack)
        string ControlID = string.Empty;
        if (!String.IsNullOrEmpty(Request.Form["__EVENTTARGET"]))
            ControlID = Request.Form["__EVENTTARGET"];
            //Buttons and ImageButtons
            if (!String.IsNullOrEmpty(Request.Form[CustomHiddenField.UniqueID]))
                ControlID = Request.Form[CustomHiddenField.UniqueID];


  • 14th December, 2010 - Truncated code with IsNullOrEmpty
  • 9th December, 2010 - Attached code and some typos corrected WTHO ThatRaja
  • 8th December, 2010 - Article published



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Hiren solanki
Software Developer
India India
He is a Smart IT devloper with Few years of Expeariance But having Great command on,C#,SQL Query,SSRS,Crystal Reports

Apart from that He Loves multimedia work too, Master of Adobe photoshop, Illustrator, CSS , HTML and all things.

He is Currently working in Microsoft Dynamics CRM and Having Nice Expearince with CRM. CRM Rocks!!!

You may also be interested in...

Comments and Discussions

GeneralA couple of suggestions Pin
Pete Sutcliffe9-Dec-10 4:38
memberPete Sutcliffe9-Dec-10 4:38 
GeneralRe: A couple of suggestions. Pin
Hiren Solanki9-Dec-10 18:55
memberHiren Solanki9-Dec-10 18:55 
GeneralMy vote of 5 Pin
marciomacedo9-Dec-10 1:44
membermarciomacedo9-Dec-10 1:44 
GeneralRe: My vote of 5 Pin
Hiren Solanki9-Dec-10 1:50
memberHiren Solanki9-Dec-10 1:50 
GeneralMy vote of 5 Pin
madhukk9-Dec-10 0:15
membermadhukk9-Dec-10 0:15 
GeneralRe: My vote of 5 Pin
Hiren Solanki9-Dec-10 0:18
memberHiren Solanki9-Dec-10 0:18 
GeneralMy vote of 1 Pin
Selvin9-Dec-10 0:09
memberSelvin9-Dec-10 0:09 
GeneralReason for me to downvote you. Pin
Hiren Solanki9-Dec-10 0:23
memberHiren Solanki9-Dec-10 0:23 
Selvin wrote:
first not an article but code snippet

Thought It's code snippets But I have spend more time to make clear understanding about it rather then putting pure snippets.

Selvin wrote:

This article is for beginner as I have also tagged it with, If you will give two of this article to beginner then I am sure they will prefer to read mine.

Anyway I am very quite sure that My article doesn't deserve 1, Giving a vote of 1 by comparing with other is not a proper way.

Rest is on you after all you all are audience,Everyone have different tastes.

GeneralRe: My vote of 1 Pin
Hiren Solanki9-Dec-10 0:26
memberHiren Solanki9-Dec-10 0:26 
GeneralMy vote of 5 Pin
Sandeep Ramani8-Dec-10 19:42
memberSandeep Ramani8-Dec-10 19:42 
GeneralRe: My vote of 5 Pin
Hiren Solanki8-Dec-10 19:51
memberHiren Solanki8-Dec-10 19:51 
GeneralMy vote of 5 Pin
thatraja8-Dec-10 18:33
memberthatraja8-Dec-10 18:33 
GeneralRe: My vote of 5 [modified] Pin
Hiren Solanki8-Dec-10 18:46
memberHiren Solanki8-Dec-10 18:46 
GeneralRe: My vote of 5 Pin
thatraja8-Dec-10 18:58
memberthatraja8-Dec-10 18:58 
Generalnice Pin
Pranay Rana8-Dec-10 8:56
memberPranay Rana8-Dec-10 8:56 
GeneralRe: nice Pin
Hiren Solanki8-Dec-10 18:20
memberHiren Solanki8-Dec-10 18:20 
GeneralMy vote of 5 Pin
Marcelo Ricardo de Oliveira8-Dec-10 6:43
memberMarcelo Ricardo de Oliveira8-Dec-10 6:43 
GeneralRe: My vote of 5 Pin
Hiren Solanki8-Dec-10 18:20
memberHiren Solanki8-Dec-10 18:20 
GeneralMy vote of 5 Pin
linuxjr8-Dec-10 5:56
memberlinuxjr8-Dec-10 5:56 
GeneralRe: My vote of 5 Pin
Hiren Solanki8-Dec-10 18:19
memberHiren Solanki8-Dec-10 18:19 
GeneralGreat snippet Pin
ThirstyMind8-Dec-10 3:44
memberThirstyMind8-Dec-10 3:44 
GeneralRe: Great snippet Pin
Hiren Solanki8-Dec-10 3:47
memberHiren Solanki8-Dec-10 3:47 
GeneralOne Suggestion Pin
Kunal_Chowdhury8-Dec-10 2:52
mentorKunal_Chowdhury8-Dec-10 2:52 
GeneralRe: One Suggestion [modified] Pin
Hiren Solanki8-Dec-10 2:54
memberHiren Solanki8-Dec-10 2:54 
GeneralMy vote of 5 Pin
Kunal_Chowdhury8-Dec-10 2:48
mentorKunal_Chowdhury8-Dec-10 2:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1509028.1 | Last Updated 14 Dec 2010
Article Copyright 2010 by Hiren solanki
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid