CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Get the Control ID that Raised PostBack in CodeBehind file of ASP.Net

By Satheesh Babu
Posted On Mar 19,2010
Article Rating:
Be first to rate
this article.
No of Comments: 2
Category: ASP.Net
Print this article.

Get the Control ID that Raised PostBack in CodeBehind file of ASP.Net

 

My previous article, Doing or Raising Postback using __doPostBack() function from Javascript in Asp.Net discussed on __doPostBack() method and its usages. In this short article, we will see how to fetch the control that raised the postback from the codebehind file.

 

As i said in my previous article, except Button and ImageButton control will use __doPostBack() javascript method to raise a postback. Button and ImageButton control uses the browsers ability and submit the form to the server.

 

Getting Controls that use __doPostBack() method

The controls that raises the postback through __doPostBack() function fills 2 hidden variables defined by ASP.Net. One of the hidden field __EVENTTARGET will be populated by the control ID of the page elements that raised the postback.

 

Hence, in this case we can fetch the control that raised the postback through the hidden field __EVENTTARGET.

 

Refer the below code,

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {    

 

        if (IsPostBack)

        {

            string controlID = Page.Request.Params["__EVENTTARGET"];

            Control postbackControl = Page.FindControl(controlID);

        }

    }

}

 

In the above code, if the postback control is a LinkButton with ID LinkButton1 then the controlID will give you LinkButton1.

 

Forcing Button to use __doPostBack() method

There is a property called UseSubmitBehaviour through which we can control the Button to use or not to use __doPostBack() method. The default value of this property is true and hence it uses browser’s ability to submit the form. When the property is set to false, then the Button control also uses __doPostBack() method. Hence, the below code is valid to fetch the Button control if it raises the postback.

ASPX

<asp:Button ID="Button1" runat="server" Text="Button"

        UseSubmitBehavior="False" />

CodeBehind

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {    

 

        if (IsPostBack)

        {

            string controlID = Page.Request.Params["__EVENTTARGET"];

            Control postbackControl = Page.FindControl(controlID);

        }

    }

}

 

Button and ImageButton control

The technique discussed in the previous section will not work for ImageButton control and a Button control by default. The other way to find a Button control or an ImageButton control is by looping the posted Request.Form collection and finding the control. One good thing that can make our life easy is, when there is multiple Button or ImageButton controls present in the form then the particular Button control or ImageButton which raised the postback will alone be posted to the server through the form collection and rest of the Button controls will be ignored. In other words, if there is multiple Button or ImageButton control that can raise postback in form then the Request.Form collection will include only one Button or ImageButton control that raised the postback.

In the Request.Form collection, if the postback element is Button control then there will be one entry for the Button control with the ID of the control. If it is an ImageButton then there will be 2 entries with ImageButtonId suffixed by .x and .y to indicate the mouse pointer location through x and y coordinates on the image when it is clicked. Look at the below visual studio QuickWatch window when an ImageButton with ID ImageButton1 is clicked.

 

How to fetch the control that initiated postback in server side using C#?

 

The below QuickWatch window shows a Button control with ID Button1.

How to fetch the control that initiated postback in server side using C#?

 

 

 Thus, if Page.Request.Params["__EVENTTARGET"] does not succeeds then find a Button or ImageButton control from the Request.Form collection.

 

Refer the below code,

 

protected void Page_Load(object sender, EventArgs e)

    {

        if (IsPostBack)

        {

             string controlID = Page.Request.Params["__EVENTTARGET"];

             Control postbackControl = null;

            if (controlID != null && controlID != String.Empty)

            {

                postbackControl = Page.FindControl(controlID);

            }

            else

            {               

                foreach (string ctrl in Page.Request.Form)

                {    //Check if Image Button

                    if (ctrl.EndsWith(".x") || ctrl.EndsWith(".y"))

                    {

                        postbackControl = Page.FindControl(ctrl.Substring(0, ctrl.Length - 2));

                        break;

                    }

                    else

                    {

                        postbackControl = Page.FindControl(ctrl);

                //Check if Button control     

  if (postbackControl is Button)

                        {

                            break;

                        }

                    }

                   

                }

            }

             Response.Write(postbackControl.ID);

        }

    }

 




Conclusion

There will be situations where we need to take some actions based on the postback control in the server and this article will help us to achieve it very easily.

Read my article, Doing or Raising Postback using __doPostBack() function from Javascript in Asp.Net to know more about raising postback using javascript.

 

Happy Coding!!

 

Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments
RE:error
Refer,
http://www.codedigest.com/Articles/ASPNET/320_Doing_or_Raising_Postback_using___doPostBack()_function_from_Javascript_in_AspNet.aspx
error
hi dear,

befor my code worked well. after copy and paste this codes, when i click on first or last button of my datapager, i get this error:
Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.