views:

549

answers:

5

I have a control which has an ImageButton which is tied to an OnClick event... Upon clicking this control, a postback is performed and the event handler is not called. AutoEventWireup is set to true, and I've double checked spelling etc.... We haven't touched this control in over a year and it has been working fine until a couple of weeks ago.

We have made changes to controls which load this control... so I'm wondering, what kind of changes could we have made to stop this event handler from being called? There is quite a bit of Javascript going on, so this could be the culprit too...

Edit: Some clarification... we are dynamically loading the parent control of the ImageButton in the OnLoad event of the page... if that makes sense.

+2  A: 

AutoEventWireup is irrelevant. Is your ImageButton loaded dynamically, i.e. not written out in mark up? If it is loaded onto the page late in the Page lifecycle e.g. in PreRender then the event will not fire.

If there is a JavaScript issue your page will not even PostBack. Is that happening?

BritishDeveloper
The page is definitely post-backing... just no events... and yes, the control looks like it is dynamic (we have a custom control which loads this).
Polaris878
In that case make sure it is put onto the page BEFORE the event handlers part of the lifecycle. This means either in OnInit or OnLoad. OnInit is better if you can since it reduces ViewState. i.e. Page.Controls.Add(myImageButton) should happen OnInit (or OnLoad)
BritishDeveloper
And is the imageButton's Click event bound OnLoad too? If so then something crazy is going on... (which I have a hack for if you are interested in filthy code)
BritishDeveloper
+1  A: 

I def agree with what BritishDeveloper said. I had a similar problem where I was dynamically loading controls, but I couldn't get a reference to the control using Page.FindControl("controlName") Someone pointed out that I needed to keep the page lifecycle in mind. I found out I needed to make sure to load the control in the PageInit because after doing an async postback the control was still there, but not loaded in the postback so there was no way to find it. This was all in csharp codebehind and ajax though, but I'm guessing the control isn't getting reloaded.

Jisaak
+1  A: 

Did you give the ImageButton an ID?

zincorp
Yep, it definitely has an ID :)
Polaris878
Do any of its parent controls (perhaps the dynamically created one) not have an ID? Most of the time when I've encountered something like this it's because somewhere along the control tree something does not have an ID assigned
zincorp
A: 

So, as it turns out we set the PostbackUrl property on one of our buttons in control A... this caused the event handlers for control B not to fire when a button in control B was pressed.

Polaris878
Hi, I seem to be having a similar problem. I was wondering if you could elaborate a bit more on your solution. Perhaps start off with what are controls A and B? Buttons? Containers? Thanks for the help in advance!
John
A: 

If you create a control dynamically. Any time you fire a postback using the new created control, you need to recreated it. Just think that your application are running at a server. How can the server hold information on controls created dynamically? Don't use Page.IsPostBack to create postback. PostbackUrl is bad solution. Workarround will be need.

Eduardo Xavier