1

I have an imagebutton which i set up at design time via the designer and assigned a method to its Click event. I need to now change that buttons target event method dynamically.

I have tried this by setting the following code but it doesn't seem to alter the target event for the imagebutton to my desired method 'imgBtnFw_Click_Details'

imgBtn.Click +=new ImageClickEventHandler(imgBtnFw_Click_Details);

Im thinking maybe i need to detach the currently assigned click event but not sure.

Does anybody have a correct set of steps for switching the target firing event method?

1
  • Try to remove initial click handler declaration from markup, and set it programmatically. Then based on the conditions you will change the event handler dynamically. Commented Nov 26, 2014 at 10:27

1 Answer 1

2

Its working...

Event Binding..

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            imgBtn.Click -= ImageButton1_Click;   // remove previous handler
            imgBtn.Click +=imgBtnFw_Click_Details; // add new handler
        }
    }

Event handler ...

 protected void imgBtnFw_Click_Details(object sender, ImageClickEventArgs e)
 {
   //your implementation
 }
Sign up to request clarification or add additional context in comments.

6 Comments

can you set up a asp.net c# web site, add image button, give it click event via the designer and then pending some objects value have it set the click event to a new method?
Fearghal, we change button event handler into page load event.
Can you explain or elaborate on your answer code? I see my code being hit so expected the click event to result in the new method being fired but no :(
if you can bind into handler into page load event then its working. protected void Page_Load(object sender, EventArgs e) { if (Page.IsPostBack) { ImageButton1.Click -= ImageButton1_Click; ImageButton1.Click += ImageButton_ClickEvent; } } Please try this ...
ahhh, that worked. So why does it need to be in page_load, why cant i put it in page_prerender or how can i put it in a method outside after page_load?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.