【问题标题】:how to retain viewstate for the dynamic controls created in GRIDVIEW如何为在 GRIDVIEW 中创建的动态控件保留视图状态
【发布时间】: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

标签: asp.net gridview


【解决方案1】:

您是否尝试通过以下方式找到它:

GridView gv = (GridView)sender;
GridViewRow gvr = (GridViewRow)gv.Rows[e.RowIndex];
(TextBox)gvr.FindControl("_registration" + e.Row.RowIndex + "_" + reg.RegistrationId.ToString())

【讨论】:

  • 我收到此错误:`'System.Web.UI.WebControls.GridViewRowCollection' 不包含'Item' 的定义,并且没有扩展方法'Item' 接受'System.'类型的第一个参数。可以找到 Web.UI.WebControls.GridViewRowCollection(您是否缺少 using 指令或程序集引用?)“System.Web.UI.WebControls.GridViewUpdateEventArgs”不包含“Row”的定义,并且没有扩展方法“Row” ' 可以找到接受“System.Web.UI.WebControls.GridViewUpdateEventArgs”类型的第一个参数(您是否缺少 using 指令或程序集引用?)`
  • 我仍然收到null;在调试时我看到gvr.DataItem 也是null
【解决方案2】:

我能够解决我的问题here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-12
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 2011-05-19
    • 1970-01-01
    相关资源
    最近更新 更多