Philippsen's Blog

Everyday findings in my world of .net and related stuff

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

One Response to “Find out which control triggered post back event”

  1. accountant said

    I believe everything said made a great deal of
    sense. However, what about this? what if you composed a catchier title?
    I am not saying your information isn’t solid, however what if you added a post title that makes people want more? I mean Find out which control triggered post back event Philippsen’s Blog is a little vanilla.
    You ought to look at Yahoo’s home page and note how they create article headlines to get viewers to click. You might add a video or a related pic or two to get readers excited about everything’ve got to say.
    Just my opinion, it could bring your blog a little livelier.

Sorry, the comment form is closed at this time.

 
%d bloggers like this: