Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

firing event in a user control

I have a page with a placeholder. I load a user control into this placeholder.

The user control has an image button on it and when I click the button, I basically want to change the image and some other info on the same control.

I've created a ImageButton1_Click function and this is correctly registered in the initializecomponent section of the user control page.

but when I click the image, nothing happens - the main page loads the user control, the user controls Load method gets called and that's it. I don't see the imageButton1_Click getting called at all.

What am I  missing?

Tuesday, March 8, 2005

You need to manually link the event, like this:
event += new eventDelegate(function)

GD (
Wednesday, March 9, 2005

And exist other thing, if you want disable event, you must put:

event -= new eventDelegate(function)

Leonardo Nunes
Thursday, March 10, 2005

I take it that you did actually set it up in InitializeComponent as was recommended above?!

Where/when does the imagebutton get added to the user control? I do recall having problems with events firing from controls that I added in the code itself (probably in InitializeComponent or similar) rather than the HTML/ascx file - think that upsets the order slightly.

Sunday, March 13, 2005

I assume, becuase you use a place holder to house your control, that you are dynamically creating the control with code and that it is not placed declaritvely in the page's ASPX file.

If that is the case, chances are that you are seing the problem because at the moment ASP.NET is processing the click, your control does not actually exist.

I would wager that you are probbably doing something like this:

if (! Page.IsPostBack)
    //Create Control

inside your load method.

However, every time that page posts back, a new object is being created. ASP.NET does a remarkable amount of work to hide that fact from you and make you think you are dealing with a single object instance across post backs, but you actually are not.

Therefore, you need to create the control every time, not just conditionally on post back.

Scott Wisniewski
Monday, March 14, 2005

*  Recent Topics

*  Fog Creek Home