【发布时间】:2011-01-25 20:48:53
【问题描述】:
我在 gridview 控件中创建动态文本框 onRowCreated 事件,但是当我尝试 findcontrol 时我得到 null
这就是我的东...
protected void gvORg_RowCreated(object sender, GridViewRowEventArgs e)
{
if ((e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate)) || (e.Row.RowState == DataControlRowState.Edit))
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
txBox txtReg = new TextBox();
txtReg.ID = "_registration" + e.Row.RowIndex + rowId.ToString();
txtReg.Text = reg.RegistrationToken;
e.Row.Cells[7].Controls.Add(txtReg);
}
}
}
protected void gvOrg_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
.....
....
TextBox _registration1 = gvOrg.Rows[e.RowIndex].Cells[7].FindControl("_registration" + e.RowIndex + rowId) as TextBox;
}
【问题讨论】:
-
为什么不使用模板?然后你可以给 TextBox 一个 ID 并使用 FindControl 方法来获取它。
-
原因是,我正在根据来自数据库的计数创建文本框,所以我不确定有多少文本框,可能是 1 或 10