views:

117

answers:

5

On postback, how can I check which control cause postback in Page_Init event.

protected void Page_Init(object sender, EventArgs e)
{
//need to check here which control cause postback?

}

Thanks

+1  A: 

Either directly in form parameters or

string controlName = this.Request.Params.Get("__EVENTTARGET");

Edit: To check if a control caused a postback (manually):

// input Image with name="imageName"
if (this.Request["imageName"+".x"] != null) ...;//caused postBack

// Other input with name="name"
if (this.Request["name"] != null) ...;//caused postBack

You could also iterate through all the controls and check if one of them caused a postBack using the above code.

Jaroslav Jandek
I have checked, its not working for me.
Muhammad Akhtar
Well then, you have to know all the controls that could have caused a postback and check if there is a value in the parameter collection (`Request[controlName]`).
Jaroslav Jandek
+1  A: 

I bet you can find your answer here: http://www.aspdotnetfaq.com/Faq/How-to-determine-which-Control-caused-PostBack-on-ASP-NET-page.aspx

richb01
And I bet that when that link ever goes dead, so will this answer.
Henk Holterman
I have checked, its not working for me
Muhammad Akhtar
@Henk Holterman Are you concerned about this particular link, or with links in general? Might we be building a vast database of soon-to-be-obsolete links? Are you advocating copying the text and code from the linked page, instead of or in addition to providing the link?
DOK
@DOK: In general a link _with_ a short summary/quote is preferred.
Henk Holterman
+1  A: 

Assuming it's a server control, you can use Request["ButtonName"]

To see if a specific button was clicked: if (Request["ButtonName"] != null)

DOK
A: 

Here's some code that might do the trick for you (taken from Ryan Farley's blog)

public static Control GetPostBackControl(Page page)
{
    Control control = null;

    string ctrlname = page.Request.Params.Get("__EVENTTARGET");
    if (ctrlname != null && ctrlname != string.Empty)
    {
        control = page.FindControl(ctrlname);
    }
    else
    {
        foreach (string ctl in page.Request.Form)
        {
            Control c = page.FindControl(ctl);
            if (c is System.Web.UI.WebControls.Button)
            {
                control = c;
                break;
            }
        }
    }
    return control;
}
silvo
+1  A: 

I see here are already great advices and methods how to get the post back control. But no post is marked as an "answer". However I found another web page (Mahesh blog) with a method to retrieve post back control ID.

I would like to post it here just as with a little modification and made it as an extension class. Hopefully it is more useful in that way.

/// <summary>
/// Gets the ID of the post back control.
/// 
/// See: http://geekswithblogs.net/mahesh/archive/2006/06/27/83264.aspx
/// </summary>
/// <param name = "page">The page.</param>
/// <returns></returns>
public static string GetPostBackControlId(this Page page)
{
    if (!page.IsPostBack)
        return string.Empty;

    Control control = null;
    // first we will check the "__EVENTTARGET" because if post back made by the controls
    // which used "_doPostBack" function also available in Request.Form collection.
    string controlName = page.Request.Params["__EVENTTARGET"];
    if (!String.IsNullOrEmpty(controlName))
    {
        control = page.FindControl(controlName);
    }
    else
    {
        // if __EVENTTARGET is null, the control is a button type and we need to
        // iterate over the form collection to find it

        // ReSharper disable TooWideLocalVariableScope
        string controlId;
        Control foundControl;
        // ReSharper restore TooWideLocalVariableScope

        foreach (string ctl in page.Request.Form)
        {
            // handle ImageButton they having an additional "quasi-property" 
            // in their Id which identifies mouse x and y coordinates
            if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
            {
                controlId = ctl.Substring(0, ctl.Length - 2);
                foundControl = page.FindControl(controlId);
            }
            else
            {
                foundControl = page.FindControl(ctl);
            }

            if (!(foundControl is Button || foundControl is ImageButton)) continue;

            control = foundControl;
            break;
        }
    }

    return control == null ? String.Empty : control.ID;
}
J Pollack