如何将动态创建的文本框值存储到db中? [英] how to store a dynamic created textbox value into db?
本文介绍了如何将动态创建的文本框值存储到db中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将组合框值传递给(2,3,4,5,6):
i pass a combobox values as(2,3,4,5,6):
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int boxes = Convert.ToInt32(comboBox1.Text);
Form1_Load(sender, e);
for (int i = 0; i < boxes; i++)
{
TextBox tb = new TextBox();
tb.Location = new System.Drawing.Point(40, 120 + i * 20);
tb.Name = "people" + i.ToString();
tb.Size = new System.Drawing.Size(184, 20);
tb.TabIndex = i + 2;
tb.Text = String.Empty;
panel1.Controls.Add(tb);
}
}
private void Form1_Load(object sender, EventArgs e)
{
panel1.Controls.Clear();
}
这里我的问题是如何将文本框值插入db。如果用户在组合框中选择3,面板显示3文本框,但如何将文本框值保存到db..note:文本框创建基于用户选择。
here my problem is how to insert textbox values to db. if user select 3 in combobox, panel show 3 textbox but how can i save textbox values into db..note: textbox create based on user selection.
推荐答案
我认为我理解你的问题。我的解决方案是在动态文本框中添加一个事件,当值发生变化时将调用该事件。
在这种情况下我真的不明白你为什么需要要做到这一点,你的texbox就是空的。
I think I understand your problem. My solution is to add an event to the dynamic textbox which will be called when the value has changed.
In this case I really don''t understand why you need to do that, your texboxes are empty.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int boxes = Convert.ToInt32(comboBox1.Text);
Form1_Load(sender, e);
for (int i = 0; i < boxes; i++)
{
TextBox tb = new TextBox();
tb.Location = new System.Drawing.Point(40, 120 + i * 20);
tb.Name = "people" + i.ToString();
tb.Size = new System.Drawing.Size(184, 20);
tb.TabIndex = i + 2;
tb.Text = String.Empty;
tb.TextChanged += new EventHandler(tb_TextChanged);
panel1.Controls.Add(tb);
}
}
void tb_TextChanged(object sender, EventArgs e)
{
//your method for insert into database
}
private void Form1_Load(object sender, EventArgs e)
{
panel1.Controls.Clear();
}
如果您打算稍后发送到db(可能是发送按钮)创建发送按钮并执行此操作:
If you plan to send to db later (maybe a send button) create your send button and do that :
void buttonSend_Click(object sender, EventArgs e)
{
foreach (Control item in panel1.Controls)
{
if(item.GetType()==typeof(TextBox) // in case you have other control types in the panel
{
yourMethod(item.Text);
}
}
}
告诉我你是否有帮助
Tell me if that help you
这篇关于如何将动态创建的文本框值存储到db中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文