简体   繁体   English

在asp.net中动态添加文本框

[英]Dynamically add textbox in asp.net

In my page, i got two column and multiple rows. 在我的页面中,我有两列和多行。 The first column contain the label such as question for the 1st row, and the options for the questions. 第一列包含标签,例如第一行的问题以及问题的选项。 And the second column is the textboxes. 第二列是文本框。 When i click on add button, i wish to add those controls to page which subsequently allowed me to add the value in the texbox to database. 当我单击添加按钮时,我希望将这些控件添加到页面,随后允许我将texbox中的值添加到数据库。 I did some research but most of them uses javascript or datatable. 我做了一些研究,但大多数使用javascript或datatable。 Is there any other method? 还有其他方法吗?

You don't specifically say what type of .net development you are doing, and your question is tagged with asp-classic, which I doubt you're using. 您没有具体说明正在执行哪种类型的.net开发,而您的问题是用asp-classic标记的,我怀疑您使用的是。 [If you are please please stop] So I will assume you are using Web Forms. [如果您需要,请停止]因此,我假设您正在使用Web窗体。

While I don't agree with Inerdial's position that you should avoid dynamic controls at all costs, I will say it does make things much more complex and requires a very good knowledge of the ASP.net Lifecycle. 尽管我不同意Inerdial的观点,即您应该不惜一切代价避免使用动态控件,但我会说它的确使事情变得更加复杂,并且需要对ASP.net生命周期有很好的了解。 If you truly want to go down that path, here is a great resource . 如果您真的想走这条路,那么这里有很多资源

With that said what you are describing to me does not appear to need that and his suggestion of setting the control visibility to false is a good one. 如此说来,您对我描述的内容似乎并不需要,他建议将控件的可见性设置为false。

You could create a row, a panel or a div and output the controls that you need when adding a new row and set it's server-side visibility to false whenever you don't want it displayed. 您可以创建一行,一个面板或一个div,并在添加新行时输出所需的控件,并在不希望显示时将其在服务器端的可见性设置为false。 Then you could have a link that when clicked it toggles the visibility to true and will allow the user to add items. 然后,您可能会有一个链接,单击该链接会将可见性切换为true,并允许用户添加项目。 Once users add items they'll be displayed in your data table and you can reuse the form to add additional items. 用户添加项目后,它们将显示在您的数据表中,您可以重复使用表单添加其他项目。

I would also like to encourage you to consider JavaScript if it isn't an overly complex form. 如果它不是一种过于复杂的形式,我也想鼓励您考虑使用JavaScript。 It eliminates an extra round-trip to your server and in general is a better user experience. 它消除了到服务器的额外往返行程,通常来说是更好的用户体验。

Edit: This link may also be of use to you. 编辑: 此链接也可能对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM