events, prerender and LoadControl()

G

Guest

I'm loading a user control using the method:

uc = Page.LoadControl("../UC.ascx");

Afterwards I set some variables in uc. For instance

uc.Count = 10;

Count is the number of link buttons which should be generated in uc. The
Pre_Render method of uc looks like this:

private void UC_PreRender(object sender, System.EventArgs e)
{
for(int i = 0; i < Count; i++)
{
LinkButton lb = new LinkButton();
lb.Text = ""+i;
lb.Click += new EventHandler(lbClick);
SomePlaceHolder.Controls.Add(lb);
}
}


The code is placed in the PreRender method because i need to be able to set
variable Count before the buttons are created. However it seems that I cannot
cathc any events on buttons which are not added in the Page_Load or before.

How do I solve this problem in a nice way?
If I could only create the usercontrol with some sort of constructer with
some arguments instead of using the LoadControl method, then my problems
would be solved.

Thanks for your help.

Nikolaj
 
K

Karl Seguin

I'm not sure why you are putting your code in PreRender..simply put it in
Page_Load and you should be good to go:

private void Page_Load(object sender, System.EventArgs e){
for (int i = 0; i < count; ++i) {
LinkButton lb = new LinkButton();
lb.Text = ""+i;
lb.Click +=new EventHandler(lb_Click);
SomePlaceHolder.Controls.Add(lb);
}
}

and in the page:
private void Page_Load(object sender, EventArgs e) {
c1 c = (c1) Page.LoadControl("c1.ascx");
c.count = 10;
Form1.Controls.Add(c);
...
}

karl
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top