如何动态生成TextBox控件。

时间:2010-02-09 12:40:48

标签: c# asp.net textbox

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

5 个答案:

答案 0 :(得分:13)

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

答案 1 :(得分:4)

以下将创建控件:

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

    }
}

答案 2 :(得分:0)

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

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

但控件(文本框)重叠。你需要组织他们的位置。

编辑: E.g

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

答案 3 :(得分:0)

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

            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 />"));

答案 4 :(得分:0)

好吧,我建议您首先创建一个网格并指定所需的行数和列数,以使所有内容井井有条。

您的 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"/>