我有 100 个文本框,我正在尝试将这些文本框中的所有文本写入文本文件,这是我的代码:
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i <= 9; i++)
{
for (int j = 0; j <= 9; j++)
{
TextBox tb = new TextBox();
tb.MaxLength = (1);
tb.Width = Unit.Pixel(40);
tb.Height = Unit.Pixel(40);
// giving each textbox a different id 00-99
tb.ID = i.ToString() + j.ToString();
Panel1.Controls.Add(tb);
}
Literal lc = new Literal();
lc.Text = "<br />";
Panel1.Controls.Add(lc);
}
}
protected void btnShow_Click(object sender, EventArgs e)
{
StringWriter stringWriter = new StringWriter();
foreach (Control control in Panel1.Controls)
{
var textBox = control as TextBox;
if (textBox != null)
{
if (string.IsNullOrEmpty(textBox.Text))
{
textBox.Style["visibility"] = "hidden";
}
// Write text to textfile.
stringWriter.Write("test.txt", textBox.Text+",");
} // end of if loop
}
}
我在 dev 文件夹中创建了一个名为 test.txt 的文件(我想它应该在哪里)它没有任何错误,但文本文件中没有任何文本。这是正确的方法吗?因为当我尝试调试时, stringWriter 的值将在第一个循环中以 test.txt 开头,在第二个循环中以 test.txttest.txt 开头。