Friday 1 April 2011

Identifying control raising postback event

This should get you the control that caused the postback:
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 Button || c is ImageButton)
            {
                control = c;
                break;
            }
        }
    }
    return control;
}

Read more about this on this page: http://ryanfarley.com/blog/archive/2005/03/11/1886.aspx

No comments:

Post a Comment