How to dynamically generate a TextBox control.

How to dynamically generate a TextBox at runtime as a result of a button click? For each button click, I would like to create a TextBox control along with the corresponding dynamic shortcuts. I would like to do this in ASP.NET using C #.

+4
source share
3 answers
TextBox txt = new TextBox(); txt.ID = "textBox1"; txt.Text = "helloo"; form1.Controls.Add(txt); Label lbl = new Label(); lbl.Text = "I am a label"; form1.Controls.Add(lbl); 
+11
source

Next, the controls will be created:

 var newTextbox = new Textbox(); var newLabel = new Label(); 

You can set the properties you need, etc.

Then find somewhere on your page to add them, let's say you have a panel named panel1, and then do the following:

 panel1.Controls.Add(newTextbox); panel1.Controls.Add(newLabel); 

However, doing this will not work after the postback - you need to recreate the dynamic controls on the back.

Say you have the following page:

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Button" /> </div> </form> </body> </html> 

When you perform the postback, only those controls that are defined on the page above will be created for you. The controls you added dynamically must be re-created by you (for example, in Page_Load).

To do this, the easiest way is to remember the total number of controls that you added to the viewstate, and then add that many controls return when a postback occurs.

The following should start:

 using System; using System.Web.UI.WebControls; namespace WebApplication1 { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { // Add any controls that have been previously added dynamically for (int i = 0; i < TotalNumberAdded; ++i) { AddControls(i + 1); } // Attach the event handler to the button Button1.Click += new EventHandler(Button1_Click); } void Button1_Click(object sender, EventArgs e) { // Increase the number added and add the new label and textbox TotalNumberAdded++; AddControls(TotalNumberAdded); } private void AddControls(int controlNumber) { var newPanel = new Panel(); var newLabel = new Label(); var newTextbox = new TextBox(); // textbox needs a unique id to maintain state information newTextbox.ID = "TextBox_" + controlNumber; newLabel.Text = "New Label"; // add the label and textbox to the panel, then add the panel to the form newPanel.Controls.Add(newLabel); newPanel.Controls.Add(newTextbox); form1.Controls.Add(newPanel); } protected int TotalNumberAdded { get { return (int)(ViewState["TotalNumberAdded"] ?? 0); } set { ViewState["TotalNumberAdded"] = value; } } } } 
+2
source

To add multiple controls for your request, use the for loop:

 for (int i = 0; i < 2; ) { TextBox textBox = new TextBox(); textBox.Text = "Hi"; textBox.Name = "textBox" + i.ToString(); form2.Controls.Add(textBox); } 

But the controls (text fields) overlap. You need to arrange their location.

EDIT: For example

 TextBox txt = new TextBox(); txt.Location = new Point(500, 100); 
0
source

Source: https://habr.com/ru/post/1300734/


All Articles