Click here to Skip to main content
16,020,424 members
Articles / Web Development / ASP.NET

Way To Know Which Control Has Raised PostBack

Rate me:
Please Sign up or sign in to vote.
4.83/5 (44 votes)
13 Dec 2010CPOL2 min read 189K   546   59   71
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.

Introduction

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.

Background

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.

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

Note:

  • 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.

HTML
<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:

C#
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:

C#
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.NET
<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.NET
<asp:Button ID="btn" runat="server" Text="Click me"
  OnClientClick = "SetSource(this.id)" />

function SetSource(SourceID)
    {
        var hidSourceID =
        document.getElementById("<%=CustomHiddenField.ClientID%>");
        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.

C#
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.

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

History

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

Reference

License

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


Written By
Software Developer
India India
He is a Smart IT devloper with Few years of Expeariance But having Great command on ASP.net,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!!!

Comments and Discussions

 
GeneralMy vote of 5 Pin
csharpbd5-Aug-13 7:23
professionalcsharpbd5-Aug-13 7:23 
GeneralRe: My vote of 5 Pin
Hiren solanki5-Aug-13 22:06
Hiren solanki5-Aug-13 22:06 
GeneralRe: My vote of 5 Pin
csharpbd6-Aug-13 6:50
professionalcsharpbd6-Aug-13 6:50 
GeneralMy vote of 4 Pin
M Rayhan14-Jul-13 19:16
M Rayhan14-Jul-13 19:16 
GeneralMy vote of 5 Pin
Prasad Khandekar27-May-13 19:53
professionalPrasad Khandekar27-May-13 19:53 
GeneralRe: My vote of 5 Pin
Hiren solanki28-Jul-13 21:01
Hiren solanki28-Jul-13 21:01 
Questionhow the server knows the submit button id caused postback Pin
karthime11-Apr-12 6:13
karthime11-Apr-12 6:13 
GeneralMy vote of 5 Pin
JF201511-Jan-11 22:43
JF201511-Jan-11 22:43 
GeneralRe: My vote of 5 Pin
Hiren solanki11-Jan-11 22:48
Hiren solanki11-Jan-11 22:48 
GeneralMy vote of 5 Pin
Abhishek Sur8-Jan-11 23:58
professionalAbhishek Sur8-Jan-11 23:58 
GeneralRe: My vote of 5 Pin
Hiren solanki9-Jan-11 19:03
Hiren solanki9-Jan-11 19:03 
GeneralMy vote of 5 Pin
TweakBird8-Jan-11 6:17
TweakBird8-Jan-11 6:17 
GeneralRe: My vote of 5 Pin
Hiren solanki9-Jan-11 19:02
Hiren solanki9-Jan-11 19:02 
GeneralMy vote of 5 Pin
RaviRanjanKr8-Jan-11 1:07
professionalRaviRanjanKr8-Jan-11 1:07 
GeneralRe: My vote of 5 Pin
Hiren solanki9-Jan-11 19:02
Hiren solanki9-Jan-11 19:02 
GeneralMy vote of 5 Pin
Abhinav S7-Jan-11 19:14
Abhinav S7-Jan-11 19:14 
GeneralRe: My vote of 5 Pin
Hiren solanki7-Jan-11 21:13
Hiren solanki7-Jan-11 21:13 
GeneralMy vote of 5 Pin
NMehta837-Jan-11 18:55
NMehta837-Jan-11 18:55 
GeneralRe: My vote of 5 Pin
Hiren solanki7-Jan-11 21:14
Hiren solanki7-Jan-11 21:14 
GeneralMy vote of 5 Pin
vikas amin4-Jan-11 10:00
vikas amin4-Jan-11 10:00 
GeneralRe: My vote of 5 Pin
Hiren solanki7-Jan-11 17:00
Hiren solanki7-Jan-11 17:00 
GeneralMy vote of 5 Pin
prasad024-Jan-11 5:02
prasad024-Jan-11 5:02 
GeneralRe: My vote of 5 Pin
Hiren solanki7-Jan-11 16:59
Hiren solanki7-Jan-11 16:59 
GeneralMy vote of 5 Pin
Abhinav S24-Dec-10 21:04
Abhinav S24-Dec-10 21:04 
GeneralRe: My vote of 5 Pin
Hiren solanki24-Dec-10 21:10
Hiren solanki24-Dec-10 21:10 

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

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