c# - 动态创建表并调整TextBox长度

时间:2011-06-27 13:34:49

标签: c# asp.net

在下面的代码中(循环中 - 未显示!),TextBox中包含的字符串的长度太长。我怎样才能解决这个问题?另外,是否可以使用TextView而不是TextBox?

TableCell tc = new TableCell();
TextBox txtBox = new TextBox();
txtBox.Text = reader.GetString(col);
// Add the control to the TableCell
tc.Controls.Add(txtBox);
// Add the TableCell to the TableRow
tr.Cells.Add(tc);
col++;

2 个答案:

答案 0 :(得分:0)

如果您想要文字,请使用Label代替TextBox。像那样:

TableCell tc = new TableCell();
Label label = new Label();
label.Text = reader.GetString(col);
// Add the control to the TableCell
tc.Controls.Add(label);
// Add the TableCell to the TableRow
tr.Cells.Add(tc);
col++;

现在,如果您想控制TextBox宽度,可以设置它:

txtBox.Width = 40; //or whatever value suits your needs

答案 1 :(得分:0)

如果您只想向用户显示文本而不希望他们对文本进行编辑,则可以使用标签控件而不是文本框。

相关问题