Philippsen's Blog

Everyday findings in my world of .net and related stuff

Posts Tagged ‘which control triggered post back’

Find out which control triggered post back event

Posted by Torben M. Philippsen on January 3, 2011

In a recent Reporting Services web project I used the ReportViewer control to display reports.

In order to position some asp.net controls properly according to whether the parameters bar was present or not, I needed to determine if the “view report” button was clicked – causing the postback. Analyzing the html output of the reportviewer control, I needed to deterrmine if the control with id “ReportViewer1_ctl00_ctl00” caused the postback.

This is what I did:

    Control ctrl = GetPostBackControl(this);

    if (ctrl.ClientID.Equals(“ReportViewer1_ctl00_ctl00”))

    {

        ImageButton1.CssClass = “”;

        LinkButton1.CssClass = “LinkButton”;

    }

 

And the code for the GetPostBackControl method:

    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)

            {

                if ((ctl.LastIndexOf(“.x”) > 0) || (ctl.LastIndexOf(“.y”) > 0))

                {

                    control = page.FindControl(ctl.Substring(0, ctl.Length – 2));

                    break;

                }

                control = page.FindControl(ctl);

                if ((control is System.Web.UI.WebControls.Button))

                {

                    break;

                }

            }

        }

        return control;

    }

Advertisements

Posted in asp.net, Reporting Services | Tagged: , | 1 Comment »

 
%d bloggers like this: