Click here to Skip to main content
15,914,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai,

I want to generate textboxes dynamically when i select a item in dropdownlist..can anybody knows please let me know
Posted

The code creates as many label controls as the user has selected from the drop-down list. The container for the controls is a PlaceHolder Web server control named Placeholder1

XML
private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
    DropDownList DropDownList1 = new DropDownList();
    PlaceHolder PlaceHolder1 = new PlaceHolder();

  // Get the number of labels to create.
 int numlabels = System.Convert.ToInt32(DropDownList1.SelectedItem.Text);
 for (int i=1; i<=numlabels; i++)
 {
   Label myLabel = new Label();

   // Set the label's Text and ID properties.
   myLabel.Text = "Label" + i.ToString();
   myLabel.ID = "Label" + i.ToString();
   PlaceHolder1.Controls.Add(myLabel);
   // Add a spacer in the form of an HTML <br /> element.
   PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
 }
}
 
Share this answer
 
v2
Comments
anand kumbham 8-Sep-11 1:23am    
Thank u Syed
private void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox txtName = new TextBox();
this.controls.Add(txtName);
}
 
Share this answer
 
Comments
angappans 8-Sep-11 4:42am    
you are correct, fine answer
Sant Osha 16-Sep-11 1:20am    
my pleasure buddy
TextBox txtName = new TextBox();
//Add the following code on the Page
this.controls.Add(txtName);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900