我正在动态创建一个包含文本框的表。我这样做与下面的代码:
foreach (DataRow row in Score_Sheet.Rows) // Loop over the rows.
{
int rowIndex = Score_Sheet.Rows.IndexOf(row); // Not sure if i need this yet
Label label = new Label();
TextBox txt = new TextBox();
txt.Text = row["Value"].ToString();
txt.ID = row["Risk"].ToString();
label.Text = row["Risk"].ToString() + " = ";
rows = new TableRow();
cell = new TableCell();
cell.Controls.Add(label);
cell2 = new TableCell();
cell2.Controls.Add(txt);
rows.Controls.Add(cell);
rows.Controls.Add(cell2);
SSGrid.Controls.Add(rows);
}
这是通过以下代码向我的网页添加表格:
<asp:Table ID="SSGrid" runat="server"></asp:Table>
该表已正确填充,但是当我尝试从表中的文本框访问更新的值时,我得到一个空引用异常。
我的查找控制代码是这样的:
TextBox txt_any = (TextBox)SSGrid.FindControl("ANY");
string anyany = txt_any.Text;
row1["Value"] = any;
如何访问此文本框中正在更新的值?
谢谢!
由于此处使用的是动态生成的控件,因此要访问它们或它们的值,必须在页面回发时重新创建它们。这是因为动态控件在视图上呈现后会丢失其状态,并且回发后再次与它们一起播放的唯一方法是在代码隐藏中重新创建它们。
因此,在这里,您只需要使用用于首次创建表的代码来重新创建表。
您可以将该if(!IsPostBack)
代码放在您的代码块之外Page_Load
,这将帮助您继续进行下去。
希望这可以帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句