Question

I want to make use of "complex" usercontrols with more than one control element within. It's the same control I will reuse in the list, and I have a PlaceHolder control for it already.

I can add the control with LoadControl(path to .ascx) - no problem.

I can through my custom properties get/set access the embedded Labels, too, so I can initialize each control perfectly.

But when adding LinkButtons, I get into trouble/problems.

When I click the button, I do get a "submit" of the page rendering the controls; but the control's own button event does not seem to fire (or at least PageLoad on the parent page seems to fire first?) - I can't figure out where my event goes or where to look for a name/ID or parameter for this button.

How come or what am I doing wrong here?

I've made a "fake button" now by using a label more with a "hardcoded A HREF" with an ID in the URL, but I would like to learn what event I need to catch and where or how to init the button, because I want to be able to use "default ASP.NET" controls for these usercontrols (hopefully without too much patchwork-coding)...

Was it helpful?

Solution

The only reason that events get "lost" is because your controls are not being recreated in such a manner that ASP.Net can associate the event with the control after the postback. It does so through the use of the ID property.

In other words, you're doing one of three things wrong:

1) You're assigning the ID's of your linkbuttons differently during the creating phase in Init after the postback

2) You're creating your linkbuttons dynamically using code, but you're doing it after the Init phase of the page lifecycle, so that your controls do not participate in ViewState.

3) You're re-binding the datasource of the parent control containing the linkbuttons on every postback. Use if (!IsPostBack) to prevent rebinding it every time.

Without seeing your code I can't give anything more specific than that unfortunately.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top