events, prerender and LoadControl()

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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
 
Back
Top