将文本框值存储到数据库中 [英] Storing Textbox values into Database
问题描述
我在做一个填字游戏,我有一个面板中有100个文本框,如下所示:
每个文本框的ID为00 - 99,因为有100个。
第一行将有一个id 00-09
,第二行将具有 10-19
的id等。
当用户在某些文本框中输入内容时,会出现 null
,并且某些文本框中将包含值。如何将值从某个ID的文本框保存到数据库?例如,上面的图片 HELP
, 22
的文本框ID将具有 H
, 23的ID 将具有
E
的值, 24
将具有 L
的值, 25
的值将具有 P 。
我不想保存 null
文本框的值,我想保存不是 null
的文本框的值。我还需要考虑他们的文本框id,以便当我填充他们,我只需要通过ID插入他们。
我是C#的新手,非常感谢任何帮助/建议/解决方案。
:
protected void Page_Load(object sender,EventArgs e)
{
//hw.Write < table>);
for(int i = 0; i <= 9; i ++)
{
//hw.Write(\"<tr>);
for(int j = 0; j <= 9; j ++)
{
TextBox tb = new TextBox();
tb.MaxLength =(1);
tb.Width = Unit.Pixel(40);
tb.Height = Unit.Pixel(40);
tb.ID = i.ToString()+ j.ToString(); //给每个文本框一个不同的id 00-99
Panel1.Controls.Add(tb);
}
Literal lc = new Literal();
lc.Text =< br />;
Panel1.Controls.Add(lc);
}
}
protected void btnShow_Click(object sender,EventArgs e)
{
foreach(Panel1.Controls中的控制控制)
{
var textBox = control as TextBox;
if(textBox!= null)
{
if(string.IsNullOrEmpty(textBox.Text))
{
textBox.Style [visibility] =隐;
}
// textBox.Enabled = false;
textBox.Text =;
}
}
}
正确的方法是将这些文本框包装在Repeater或Datalist控件中。您可以通过此处准备好这些控件。这样,当行数增加时,您不必更改为循环或硬编码值。
对于您存储给定Id的值的问题,可以在数据库中定义行#和col#,并在行#和col#上排序,这应该可以工作。
I am doing a crossword puzzle and i have 100 text boxes in a panel like this :
Every text box have an id of 00 - 99 since there is 100 of it .
First Row will have an id 00-09
, 2nd row will have an id of 10-19
and so on.
When user types something in some text box will be null
and some text box will have values in it. How do I save values from a text box of a certain id to a database? For example the above image, HELP
, text box id of 22
will have the value H
, id of 23
will have the value of E
, id of 24
will have value of L
, id of 25
will have value of P
.
I don't want to save the null
values of the text box , I want to save values of the textboxes which are not null
. I also need to take into account their textbox ids so that when I populate them back, I just have to insert them through ID .
I am new to C# , appreciate any help/advise/solutions on this.
Here is my code:
protected void Page_Load(object sender, EventArgs e)
{
//hw.Write("<table>");
for (int i = 0; i <= 9; i++)
{
//hw.Write("<tr>");
for (int j = 0; j <= 9; j++)
{
TextBox tb = new TextBox();
tb.MaxLength = (1);
tb.Width = Unit.Pixel(40);
tb.Height = Unit.Pixel(40);
tb.ID = i.ToString() + j.ToString(); // giving each textbox a different id 00-99
Panel1.Controls.Add(tb);
}
Literal lc = new Literal();
lc.Text = "<br />";
Panel1.Controls.Add(lc);
}
}
protected void btnShow_Click(object sender, EventArgs e)
{
foreach (Control control in Panel1.Controls)
{
var textBox = control as TextBox;
if (textBox != null)
{
if (string.IsNullOrEmpty(textBox.Text))
{
textBox.Style["visibility"] = "hidden";
}
// textBox.Enabled = false;
textBox.Text = "";
}
}
}
The proper way to do this is to wrap these textboxes inside a Repeater or Datalist controls. You can ready about these controls from here. This way when number of rows increase you will not have to change to your loop or hard-coded values.
As for your question to store values for a given Id, you can define row# and col# in your database and sort on row# and col#, this should work.
这篇关于将文本框值存储到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!