User Controls

  • Thread starter Charles A. Lackman
  • Start date
C

Charles A. Lackman

Hello,

I have created a User Control within Visual Studio and it contains a
button that allows the user to querry a database. I dynamically add
additional controls to the page based on the number of rows retured inside a
dataset. All works well, but I have noticed that the control is rendered
in the "Pre Render" event I loose the Control's events (i.e. button1.click).

Is there a way to render the controls in the "Pre Render" event and still
maintain the controls Events?

Dim HowMany as Int16 = 8
Dim MyControl(HowMany) As UserControl
MyControl(i) = LoadControl("ArtistControl.ascx")
Page.FindControl("Form1").Controls.Add(MyControl(i))

The Control Must be an array of controls because "HowMany" changes for each
page. If I load the control in the "Page Load" event the controls events
work. Is this a bug or am I missing something. I even added an Event
Handler to the control with no success.

Any Suggestions would be greatly appreciated,

Thanks,
Chuck
 
M

Marshal Antony

Hi Charles,

Page_Init will be the better choice for handling events in your
case.Page_Load also will work fine.
The web form triggers 11 events when it is loaded.Custom
controls follow the same sequence as well.
The order in which events are triggered are as follows :

Initialization,Load View State,Post Back Data
Processing,Page Load,Post Back Change Notification,Post Back event
processing,
Pre-rendering,Save View state,Rendering,Disposing,Unload.
If you look at the order, Pre- render comes after Load View State
and Post Back event processing.
If you put the code in Pre-render for control loading,as the page life cycle
happens for each request,the controls will render but
at this point the post back event has already been processed.
When you click on the button a post back occured and it
is trying to access the control which doesn't exist because
your loading of the control happens after the Post Back event processing.
So put your code in Page_Init or Page_Load.

Hope this helps.
Regards,
Marshal Antony
..NET Developer
http://www.dotnetmarshal.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top