【问题标题】:How to dynamically generate a TextBox control.如何动态生成 TextBox 控件。
【发布时间】:2021-03-24 23:40:57
【问题描述】:

如何在运行时通过单击按钮动态生成 TextBox 控件?对于每个按钮单击,我想创建一个 TextBox 控件以及相应的动态标签。我想使用 C# 语言在 ASP.NET 中执行此操作。

【问题讨论】:

    标签: c# asp.net textbox


    【解决方案1】:
    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);
    

    【讨论】:

    • protected void Button1_Click(object sender, EventArgs e) { TextBox txt = new TextBox(); txt.ID = "文本框1"; txt.Text = "你好"; form1.Controls.Add(txt);标签 lbl = 新标签(); lbl.Text = "我是一个标签"; form1.Controls.Add(lbl);这给了我一个文本框和标签,但我需要为我点击的每个按钮生成这些
    • 更改每个文本框的 id,因为 id 名称在文档中应该是唯一的。
    【解决方案2】:

    以下将创建控件:

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

    然后您可以设置所需的属性等。

    然后在页面上的某个位置添加它们,假设您有一个名为 panel1 的面板,然后执行以下操作:

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

    但是,在回发后这样做将不起作用 - 您需要在回发时自己重新创建动态控件。

    假设您有以下页面:

    <%@ 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>
    

    当您进行回发时,只会为您生成上述页面中定义的控件。您需要重新创建您动态添加的控件(例如在 Page_Load 中)。

    要做到这一点,最简单的方法是记住您在视图状态中添加的控件总数,然后在回发发生时重新添加那么多控件。

    以下内容应该可以帮助您入门:

    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; }
            }
    
        }
    }
    

    【讨论】:

    • “您动态添加的控件需要您重新创建”:是否有关于此行为的解释?
    • 哇,这个问题的最佳解释和这个问题的整体。老实说,应该是公认的答案。
    【解决方案3】:

    要按您的要求添加多个控件,请使用 for 循环:

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

    但控件(文本框)重叠。您需要整理他们的位置。

    编辑: 例如

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

    【讨论】:

      【解决方案4】:

      下面的代码显示了如何根据下拉列表中选择的值打印标签和文本框。使用这两个占位符,以便它们可以适当地放置在两个不同的表格分区中

                  int numlabels = System.Convert.ToInt32(ddlNumOfVolunteers.SelectedItem.Text);
                  for (int i = 1; i <= numlabels; i++)
                  {
                      Label myLabel = new Label();
                      TextBox txtbox = new TextBox();
                      // Set the label's Text and ID properties.
                      myLabel.ID = "LabelVol" + i.ToString();
                      myLabel.Text = "Volunteer " + i.ToString();
                      txtbox.ID = "TxtBoxVol" + i.ToString();
                      PlaceHolder1.Controls.Add(myLabel);
                      PlaceHolder2.Controls.Add(txtbox);
                      // Add a spacer in the form of an HTML <br /> element.
                      PlaceHolder2.Controls.Add(new LiteralControl("<br />"));
                      PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
      

      【讨论】:

      • 请将该解释放在您的答案本身中,而不是在评论中。
      【解决方案5】:

      我建议您首先创建一个网格并指定所需的行数和列数,以便让一切井井有条。

      您的 MainWindow.xaml 应如下所示:

       <TabItem.Background>
                      <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                          <GradientStop Color="#FFF0F0F0" Offset="1"/>
                          <GradientStop Color="#FF111111"/>
                          <GradientStop Color="#FF4F2B2B" Offset="0.946"/>
                      </LinearGradientBrush>
                  </TabItem.Background>
      
                  <ScrollViewer HorizontalScrollBarVisibility="Visible" Margin="0,-1,0,2">
      
         <Grid Name="gridaxis"  x:FieldModifier="private" >
                          <Grid.RowDefinitions>
                              <RowDefinition />
                              <RowDefinition />
                              <RowDefinition />
      
                          </Grid.RowDefinitions>
                          <Grid.ColumnDefinitions>
                              <ColumnDefinition />
                              <ColumnDefinition />
                              <ColumnDefinition />
                              <ColumnDefinition />
                              <ColumnDefinition />
                              <ColumnDefinition />
                              <ColumnDefinition />
                              <ColumnDefinition />
                              <ColumnDefinition />
                              <ColumnDefinition />
                              <ColumnDefinition />
                              <ColumnDefinition />
                              <ColumnDefinition />
                              <ColumnDefinition />
                              <ColumnDefinition />
                              <ColumnDefinition />
                              <ColumnDefinition />
                              <ColumnDefinition />
                              <ColumnDefinition />
                              <ColumnDefinition />
                              <ColumnDefinition />
                              <ColumnDefinition />
                              <ColumnDefinition />
                              <ColumnDefinition />
                              <ColumnDefinition />
                              <ColumnDefinition />
                              <ColumnDefinition />
                          </Grid.ColumnDefinitions>
                      </Grid>
      
      
                  </ScrollViewer>
      
              </TabItem>
      

      接下来我们正在尝试构建两行文本框,它们将在运行时动态创建。通过使用两个不同的按钮,一个用于第一行文本框,另一个用于第二行,我们将能够生成所需的文本框。

       int i = 0;
          private void add_in_grid(object sender, RoutedEventArgs e)
          {
              i = i + 1;
              System.Windows.Controls.TextBox newDepo = new System.Windows.Controls.TextBox();
      
              newDepo.Name = "new_Depo_" + i;
              newDepo.Text = "neue Depo" + i;
      
              newDepo.Width = 200;
              newDepo.Height = 200;
      
              Grid.SetColumn(newDepo, i);
              Grid.SetRow(newDepo, 1);
      
              gridaxis.Children.Add(newDepo);
          }
      
          int k = 0;
          private void add_row_depo(object sender, RoutedEventArgs e)
          {
              k = k + 1;
              System.Windows.Controls.TextBox newRowDepo = new System.Windows.Controls.TextBox();
              newRowDepo.Name = "newRowDepo" + k;
              newRowDepo.Text = "row depo" + k;
      
      
      
              newRowDepo.Width = 200;
              newRowDepo.Height = 200;
      
              Grid.SetColumn(newRowDepo, k);
              Grid.SetRow(newRowDepo, 2);
              gridaxis.Children.Add(newRowDepo);
          }
      

      我希望它足够清楚。您只需为上面列出的两种方法中的每一种添加一个按钮。就像这样:

       <Button x:Name="grid_textbox" Content="add using grid" Click="add_in_grid" HorizontalAlignment="Left" Margin="802,537,0,0" VerticalAlignment="Top" Width="197" Height="60"/>
          <Button x:Name="rowgrid" Content="add row" Click="add_row_depo" HorizontalAlignment="Left" Margin="617,537,0,0" VerticalAlignment="Top" Width="159" Height="65"/>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-01
        • 2010-11-04
        • 2012-03-21
        • 2013-09-04
        • 1970-01-01
        相关资源
        最近更新 更多