如何在ASP.NET中动态添加文本框?

时间:2022-05-28 08:41:00

I've a following requirement for my asp.net page:

我的asp.net页面有以下要求:

  1. User can add a textbox dynamically on a page A by clicking on link "Add a new category" hyperlink

    用户可以通过单击链接“添加新类别”超链接在页面A上动态添加文本框

  2. He clicks submit button on page A and gets redirected to page B.

    他点击了第A页的提交按钮,并被重定向到了第B页。

  3. When he clicks on page A link from this page, the textboxes that he added should be persisted.

    当他点击此页面上的页面A链接时,应该保留他添加的文本框。

Can someone help me with the code on this?

有人可以帮我解决这个问题吗?

Appreciate your help!

感谢您的帮助!

5 个解决方案

#1


In the ButtonClick Method write.

在ButtonClick方法中写。

TextBox tb = new TextBox();

Parent.Controls.Add( tb );

The Parent is the control you want to add the textbox to, for instance a panel.

Parent是您要将文本框添加到的控件,例如面板。

You can also look at this resource.

您还可以查看此资源。

Hope it helps.

希望能帮助到你。

#2


Adding a user control dynamically is simple. But in this case, I don't think you need to do that, instead you should look at creating a repeater with a textbox inside it, and when the user clicks Add Category, add one item to the repeater datasource.

动态添加用户控件很简单。但在这种情况下,我认为您不需要这样做,而是应该考虑创建一个带有文本框的转发器,当用户单击“添加类别”时,将一个项目添加到转发器数据源。

This way you can handle both control creation and state persistence at the same time.

这样,您可以同时处理控件创建和状态持久性。

#3


Dynamically creating Textboxes:

动态创建文本框:

suppose you have page like this如何在ASP.NET中动态添加文本框?

假设你有这样的页面

when you enter '1' in textbox and click on ADD button,output will be like display one textbox below.. i have designed like this, 如何在ASP.NET中动态添加文本框?

当您在文本框中输入“1”并单击“添加”按钮时,输出将类似于显示下面的一个文本框..我的设计是这样的,

i have one textbox and placeholder for displaying dynamic textboxes.. double click on Add button...in btnadd_click,you have to write the following code

我有一个文本框和占位符用于显示动态文本框..双击添加按钮...在btnadd_click中,你必须编写以下代码

protected void btnadd_Click(object sender, EventArgs e) {

protected void btnadd_Click(object sender,EventArgs e){

    int i;
       for (i = 0; i < Convert.ToInt32(txtno.Text); i++)

    {

        TextBox txtbox = new TextBox();
        phtxt.Controls.Add(txtbox);

        phtxt.Controls.Add(new LiteralControl("<br>"));

    }

  }

debug it... and the output is, 如何在ASP.NET中动态添加文本框?

调试它...输出是,

#4


As others have stated adding the text box dynamically is fairly straight forward, just create the Textbox and add it to the controls collections wherever you need it to show up. You then need to store the information that this user gets this additional text box. Assuming that this is meant for long term, you will need to store this information in your backend store. Whenever you are constructing the page you will need to read the store information first to see what textboxes to create.

正如其他人所说,动态添加文本框非常简单,只需创建文本框并将其添加到控件集合中,无论您需要它出现在哪里。然后,您需要存储此用户获取此附加文本框的信息。假设这是长期的,您需要将此信息存储在后端存储中。无论何时构建页面,您都需要首先阅读商店信息,以查看要创建的文本框。

I would suggest doing it as follows. In the Onload event, if you have not done so before, load the dynamic information from your DB. Add any necessary controls to the page and store this information in viewstate. On any subsequent postbacks, read the information from viewstate to add the additional controls. This will save you from having to read constantly from the database on each postback.

我建议按如下方式进行。在Onload事件中,如果您之前没有这样做,请从数据库加载动态信息。向页面添加任何必要的控件,并将此信息存储在viewstate中。在任何后续回发中,从viewstate中读取信息以添加其他控件。这将使您不必在每次回发时不断从数据库中阅读。

#5


dealing with dynamic user controls can be a pain in the ass.

处理动态用户控件可能是一个痛苦的屁股。

as a rule of thumb i follow, whenever you create a dynamic user control, then you must set it's ID so ASP.net can reallocate it on post back, and to keep the controls values after post back you should reload your user controls on Page_Init event.

根据我的经验,每当你创建一个动态用户控件时,你必须设置它的ID,以便ASP.net可以在回发后重新分配它,并在发回后保留控件值,你应该重新加载Page_Init上的用户控件事件。

hope this helps.

希望这可以帮助。

#1


In the ButtonClick Method write.

在ButtonClick方法中写。

TextBox tb = new TextBox();

Parent.Controls.Add( tb );

The Parent is the control you want to add the textbox to, for instance a panel.

Parent是您要将文本框添加到的控件,例如面板。

You can also look at this resource.

您还可以查看此资源。

Hope it helps.

希望能帮助到你。

#2


Adding a user control dynamically is simple. But in this case, I don't think you need to do that, instead you should look at creating a repeater with a textbox inside it, and when the user clicks Add Category, add one item to the repeater datasource.

动态添加用户控件很简单。但在这种情况下,我认为您不需要这样做,而是应该考虑创建一个带有文本框的转发器,当用户单击“添加类别”时,将一个项目添加到转发器数据源。

This way you can handle both control creation and state persistence at the same time.

这样,您可以同时处理控件创建和状态持久性。

#3


Dynamically creating Textboxes:

动态创建文本框:

suppose you have page like this如何在ASP.NET中动态添加文本框?

假设你有这样的页面

when you enter '1' in textbox and click on ADD button,output will be like display one textbox below.. i have designed like this, 如何在ASP.NET中动态添加文本框?

当您在文本框中输入“1”并单击“添加”按钮时,输出将类似于显示下面的一个文本框..我的设计是这样的,

i have one textbox and placeholder for displaying dynamic textboxes.. double click on Add button...in btnadd_click,you have to write the following code

我有一个文本框和占位符用于显示动态文本框..双击添加按钮...在btnadd_click中,你必须编写以下代码

protected void btnadd_Click(object sender, EventArgs e) {

protected void btnadd_Click(object sender,EventArgs e){

    int i;
       for (i = 0; i < Convert.ToInt32(txtno.Text); i++)

    {

        TextBox txtbox = new TextBox();
        phtxt.Controls.Add(txtbox);

        phtxt.Controls.Add(new LiteralControl("<br>"));

    }

  }

debug it... and the output is, 如何在ASP.NET中动态添加文本框?

调试它...输出是,

#4


As others have stated adding the text box dynamically is fairly straight forward, just create the Textbox and add it to the controls collections wherever you need it to show up. You then need to store the information that this user gets this additional text box. Assuming that this is meant for long term, you will need to store this information in your backend store. Whenever you are constructing the page you will need to read the store information first to see what textboxes to create.

正如其他人所说,动态添加文本框非常简单,只需创建文本框并将其添加到控件集合中,无论您需要它出现在哪里。然后,您需要存储此用户获取此附加文本框的信息。假设这是长期的,您需要将此信息存储在后端存储中。无论何时构建页面,您都需要首先阅读商店信息,以查看要创建的文本框。

I would suggest doing it as follows. In the Onload event, if you have not done so before, load the dynamic information from your DB. Add any necessary controls to the page and store this information in viewstate. On any subsequent postbacks, read the information from viewstate to add the additional controls. This will save you from having to read constantly from the database on each postback.

我建议按如下方式进行。在Onload事件中,如果您之前没有这样做,请从数据库加载动态信息。向页面添加任何必要的控件,并将此信息存储在viewstate中。在任何后续回发中,从viewstate中读取信息以添加其他控件。这将使您不必在每次回发时不断从数据库中阅读。

#5


dealing with dynamic user controls can be a pain in the ass.

处理动态用户控件可能是一个痛苦的屁股。

as a rule of thumb i follow, whenever you create a dynamic user control, then you must set it's ID so ASP.net can reallocate it on post back, and to keep the controls values after post back you should reload your user controls on Page_Init event.

根据我的经验,每当你创建一个动态用户控件时,你必须设置它的ID,以便ASP.net可以在回发后重新分配它,并在发回后保留控件值,你应该重新加载Page_Init上的用户控件事件。

hope this helps.

希望这可以帮助。