如何将动态创建的文本框值插入数据库 [英] How Do I Insert Dynamically Created Textbox Values To Database
本文介绍了如何将动态创建的文本框值插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< asp:Panel ID = Panel1 runat = server >
< / asp:Panel >
< asp:按钮 ID = Button1 runat = server 文本 = 按钮 OnClick = Button1_Click / >
我的default.aspx.cs文件代码
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControls;
public partial class test_Default2:System.Web.UI.Page
{
protected void Page_load(object sender,EventArgs e)
{
if( !Page.IsPostBack)
{
//首次加载页面时删除会话。
Session.Remove(点击);
}
}
protected void Button1_Click(object sender,EventArgs e)
{
int rowCount = 0;
//初始化会话。
rowCount = Convert.ToInt32(Session [clicks]);
rowCount ++;
//在每个按钮中,将数字保存到会话中。
Session [clicks] = rowCount;
//每次单击按钮时创建文本框和标签。
for(int i = 0; i< rowCount; i ++)
{
TextBox TxtBoxU = new TextBox();
TextBox TxtBoxE = new TextBox();
Label lblU = new Label();
Label lblE = new Label();
TxtBoxU.ID =TextBoxU+ i.ToString();
TxtBoxE.ID =TextBoxE+ i.ToString();
lblU.ID =LabelU+ i.ToString();
lblE.ID =LabelE+ i.ToString();
lblU.Text =User+(i + 1).ToString()+:;
lblE.Text =电子邮件:;
//将标签和文本框添加到Panel。
Panel1.Controls.Add(lblU);
Panel1.Controls.Add(TxtBoxU);
Panel1.Controls.Add(lblE);
Panel1.Controls.Add(TxtBoxE);
}
}
}
解决方案
嗨
$ b提交按钮中的$ b单击使用以下代码从动态创建的文本框中检索文本
protected void btnSubmit_Click( object sender,EventArgs e)
{
TextBox txt;
foreach (控制c Panel1.Controls)
{
if (c.GetType()== typeof (TextBox))
{
txt =(TextBox)c;
String str = txt.Text;
}
}
}
为每个文本框控件分配唯一ID ....对于唯一ID使用GUID生成器
请将文本框的(动态)id存储在列表与LT;串GT;然后按名称访问动态创建的文本框...
谢谢,
Ullas克里希南
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
my default.aspx.cs file code
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class test_Default2 : System.Web.UI.Page { protected void Page_load(object sender, EventArgs e) { if (!Page.IsPostBack) { //Remove the session when first time page loads. Session.Remove("clicks"); } } protected void Button1_Click(object sender, EventArgs e) { int rowCount = 0; //initialize a session. rowCount = Convert.ToInt32(Session["clicks"]); rowCount++; //In each button clic save the numbers into the session. Session["clicks"] = rowCount; //Create the textboxes and labels each time the button is clicked. for (int i = 0; i < rowCount; i++) { TextBox TxtBoxU = new TextBox(); TextBox TxtBoxE = new TextBox(); Label lblU = new Label(); Label lblE = new Label(); TxtBoxU.ID = "TextBoxU" + i.ToString(); TxtBoxE.ID = "TextBoxE" + i.ToString(); lblU.ID = "LabelU" + i.ToString(); lblE.ID = "LabelE" + i.ToString(); lblU.Text = "User " + (i + 1).ToString() + " : "; lblE.Text = "E-Mail : "; //Add the labels and textboxes to the Panel. Panel1.Controls.Add(lblU); Panel1.Controls.Add(TxtBoxU); Panel1.Controls.Add(lblE); Panel1.Controls.Add(TxtBoxE); } } }
解决方案
Hi
in the submit button click use the following code to retrieve texts from dynamically created textboxes
protected void btnSubmit_Click(object sender, EventArgs e) { TextBox txt; foreach (Control c in Panel1.Controls) { if (c.GetType() == typeof(TextBox)) { txt = (TextBox)c; String str = txt.Text; } } }
Hi,
Assign unique ID for each Textbox control.... for unique id use GUID generator
Please store the (dynamic) id of text box in a List<string> and then access the dynamically created text-box by its name...
Thanks,
Ullas Krishnan
这篇关于如何将动态创建的文本框值插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文