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

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