protected override void CreateChildControls()

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.
 
J

John Saunders

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?
 
J

Jon

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.
 
J

John Saunders

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.
 

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,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top