Determine which button inside the custom control dispatched the event

I have a custom control with multiple buttons.

Q page_load

, I want to run a method if a specific button has not been clicked.

When I check the sender for page_load

inside the custom control, I just get the name of the custom control, not the button itself.

Is there a way to determine which button was clicked on page_load

? Otherwise, I will have to come up with a hacky method to solve the problem.

+1


source to share


4 answers


I think you can check Request.Form ("__EVENTTARGET")

which should contain ClientID

your control.



This refers to the hidden field value used by the ASP.NET event handling framework to track what the user has clicked. When the user triggers the feedback, some JavaScript on the page sets this hidden field to the ClientID

control you clicked before submitting the form.

+2


source


Can you create a property in your custom control to return the clicked button (or set a flag or whatever), set it on every button click event inside the custom control?



0


source


Are you sure you are working with the page model correctly? Page Load event (to create a server side object model), then your control will handle the button click event associated with the control.

Page loading can occur for any number of feedback reasons, with the exception of button clicks in a custom control.

How about buttons in other controls on the page?

There are sometimes good reasons for doing this, but I am also worried that you are just hacking into an ASP.NET page page.

0


source


Here's a simple way to check if a specific button was clicked:

protected bool isButtonClicked(string buttonName)
    {
        bool isClicked = false;
        foreach (string ctl in this.Request.Form)
        {
            if (ctl.EndsWith(buttonName))
            {
                isButtonClicked = true;
                break;
            }
        }
        return isClicked;
    } 

      

0


source







All Articles