如何动态添加文本框并保存在asp.net中 [英] how to add textbox dynamically and save in asp.net
问题描述
我想动态添加文本框,并将这些值保存到asp.net中的数据库中,asp.net中是否对此有任何控件?
I want to add textboxes dynamically and save these values to database in asp.net, Is there any controls for this in asp.net?
我如何在asp.net中做到这一点?
how can I do this in asp.net?
推荐答案
在下面的.aspx文件中添加动态控件的示例代码.
To add dynamic control in your .aspx file below is sample code.
在.aspx文件中的div下方添加id为addControl的
Add below div in your .aspx file with id which is addControl
<div id="addControl" runat="server">
</div>
您可以通过在.cs文件中添加以下代码来创建动态TextBox控件.添加控件是上面定义的div的ID.
You can create dynamic TextBox control by adding below code in you .cs file. Add Control is a id of above define div.
TextBox txt1 = new TextBox();
txt1.ID = "txtOne";
txt1.Text = "";
txt1.CssClass = "myClass";
txt1.ClientIDMode = System.Web.UI.ClientIDMode.Static;
addControl.Controls.Add(txt1);
借助NameValueCollection,您可以获取TextBox的值.
With the help of NameValueCollection you can get the value of TextBox.
NameValueCollection frmCollection = Request.Form;
string inputString = frmCollection["txtOne"];
NameValueCollection表示关联的字符串键的集合和字符串值,可以通过键或通过索引.
NameValueCollection represents a collection of associated String keys and String values that can be accessed either with the key or with the index.
这篇关于如何动态添加文本框并保存在asp.net中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!