Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create new objects in runtime

Status
Not open for further replies.

MJB3K

Programmer
Jul 16, 2004
524
GB
Hi, I am trying to create a new label at runtime and give it a unique reference.

Is this possible? Below is the code i have started with... (although it doesn't work yet)
Code:
for (int i = 0; i < 5; i++)
{
    Label label + i = new Label();
}
As you can see, i am tyring to replicate a label and give it a unique name (the 5 could be any number... is this the right way of doing it, or is there a better way?

Regards,

Martin

Computing Design And Services:
 
that works. you would want to assign an ID to the label and add the control to the given collection (Page, table, cell, panel, etc)

this must also happen with each postback.
Code:
protected override void OnInit(EventArgs e)
{
   base.OnInit(e);
   for(int i = 0; i < 5; i++)
   {
       Label label = new Label();
       label.ID = "dynamicLabel_" + i;
       label.Text = i.ToString();
       Controls.Add(label);
   }
}
this will add 5 controls add append them to the Page.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top