protected override void CreateChildControls()

  • Thread starter Thread starter Jon
  • Start date Start date
J

Jon

Hi!
I'm creating a custom Server Control which implements the method in the
subject line (Protected override void CreateChildControls)

In here I have the following code:

LBL myLbl = new LBL();
myLbl.ID = "MyLbl";
myLbl.Text = objCustomFieldUtil.GetName("custom");
this.Controls.Add(new LiteralControl("<td class=\"regLabelCell\">"));
this.Controls.Add(myLbl);
this.Controls.Add(new LiteralControl("</td>"));

Now what I'd like to do is add the child controls dynamically by looping
through a Data Table:

for(int i = 0; i < objDT.Rows.Count; i++)
{
//Implement Adding Controls here
}

The problem arises that I need to declare my label or textbox before I can
added it. Anyone know a way around this?

Thank you.
 
Jon, it's not a problem:

for(into I = 0; I < bond.Rows.Count; I++)
{
Label aspLabel = new Label();
aspLabel.ID = "Label" + I.ToString();
aspLabel.Text = bond.Rows["field"];
this.Controls.Add(aspLabel);
}

Of course, you want to do this in the right place, and maybe you want to
enclose each label in a "<td></td>".
--
John Saunders
Internet Engineer
(e-mail address removed)

P.S. Did you do this.Controls.Clear() at the beginning of
CreateChildControls?
 
John,
Thanks...Sorry for not trying that first (makes me look like a
dumba$$ In my defense it was late and I was on the way out :-) )

I did not do this.Controls.Clear(), what does that do exactly...remove all
references to child controls on the postback?

Thanks,

Jon

John Saunders said:
Jon, it's not a problem:

for(into I = 0; I < bond.Rows.Count; I++)
{
Label aspLabel = new Label();
aspLabel.ID = "Label" + I.ToString();
aspLabel.Text = bond.Rows["field"];
this.Controls.Add(aspLabel);
}

Of course, you want to do this in the right place, and maybe you want to
enclose each label in a "<td></td>".
--
John Saunders
Internet Engineer
(e-mail address removed)

P.S. Did you do this.Controls.Clear() at the beginning of
CreateChildControls?

Jon said:
Hi!
I'm creating a custom Server Control which implements the method in the
subject line (Protected override void CreateChildControls)

In here I have the following code:

LBL myLbl = new LBL();
myLbl.ID = "MyLbl";
myLbl.Text = objCustomFieldUtil.GetName("custom");
this.Controls.Add(new LiteralControl("<td class=\"regLabelCell\">"));
this.Controls.Add(myLbl);
this.Controls.Add(new LiteralControl("</td>"));

Now what I'd like to do is add the child controls dynamically by looping
through a Data Table:

for(int i = 0; i < objDT.Rows.Count; i++)
{
//Implement Adding Controls here
}

The problem arises that I need to declare my label or textbox before I can
added it. Anyone know a way around this?

Thank you.
 
Jon said:
John,
Thanks...Sorry for not trying that first (makes me look like a
dumba$$ In my defense it was late and I was on the way out :-) )

I did not do this.Controls.Clear(), what does that do exactly...remove all
references to child controls on the postback?

Controls.Clear() removes all of your child controls. You do it at the
beginning of CreateChildControls to ensure that the set of child controls is
exaclty the set created in CreateChildControls.

Now, I never quite tracked it down, but I've had problems with
CreateChildControls being called twice. Calling Controls.Clear solved the
problem.
 
Back
Top