How to create Dynamic Control(Label) in asp.net with c#

M

Manoj Mittal

I have created the Dynamic control in asp.net c#
like

for (int i = 1; i <= 5; i++)
{
Label label = new Label();
label.ID = "Label" + i;
label.Text = "Label"+i;
form1.Controls.Add(label);
}

problem is how to get label1or label2 etc control outside this
function..it is accessiable outside this..plz help me in solving this
problem
 
D

David R. Longnecker

You should be able to access the controls using the FindControl method.

for (int i = 1; i <= 5; i++)
{
Label label = new Label();
label.ID = "Label" + i;
label.Text = "Label" + i;
form1.Controls.Add(label);
}

((Label)form1.FindControl("Label1")).Text = "Hello, Label 1!<br/>";
((Label)form1.FindControl("Label2")).Text = "Hello, Label 2!<br/>";
((Label)form1.FindControl("Label3")).Text = "Hello, Label 3!<br/>";
((Label)form1.FindControl("Label4")).Text = "Hello, Label 4!<br/>";
((Label)form1.FindControl("Label5")).Text = "Hello, Label 5!<br/>";

HTH.

-dl
 

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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top