为什么GridViewRow的“FindControl”找不到下拉列表?

两个例外:

  1. 索引超出范围
  2. FindControl返回null(它假装或不检测控件)

cs代码:(现在只需要在编辑模式下填充dropdownlist)

protected void GridView3_RowEditing(object sender, GridViewEditEventArgs e) { GridView3.EditIndex = e.NewEditIndex; ShowData("a"); //bind data GridViewRow gVR = GridView3.Rows[GridView3.EditIndex]; 

aspx代码:

    <asp:DropDownList ID="xnList" runat="server" Text=''>    <asp:Label ID="Label3" runat="server" Text=''>    

鉴于上面的代码片段,就在第3行,我得到了以下错误。 这是荒谬的,因为同样适用于其他gridview,这个gridview有10行,所以绝对不会超出界限。 这可能是什么问题?

参考文献:

  • 在EDIT模式下在GridView中查找控件将返回null。
  • gridview中的FindControl返回null

编辑

那些慷慨地试图帮助我解决问题的人, 请查看Jeff Atwood关于Page.FindControl的博客文章。 阅读它,我觉得我的下拉列表绝对是Gridview中的一个孩子… 鉴于这篇文章,它更接近我遇到的。 。 但我不是百分百肯定,如果同样的情况适用于我正在努力的事情,因为我有两个网格视图。 但是只有一个具有编辑模式控件 – 另一个是纯普通gridvos。 有人能告诉我正确的方向吗?

编辑 :我已经尝试了以上链接的每个答案/解决方案。 截至目前没有工作。

正如许多人已经指出的那样,RowDataBound()是为gridview中的控件挂起数据以进行编辑,更新或显示模式的正确事件。 我很绝望,然后尝试了Row_Updating。 我不知道错误的问题。

这主要是由于Text='<%# Bind("[columnx]")%>'

  

所以最终的解决方案是根据那里发布的任何答案。

CS:

  protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if ((e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit) { DropDownList ddl = e.Row.FindControlRecursive("dhl") as DropDownList; DropDownList stageDDL = e.Row.FindControlRecursive("dhl") as DropDownList; stageDDL.DataSource = this.clservice.Getstuff("someparam"); stageDDL.DataTextField = "columnx"; stageDDL.DataValueField = "columnx"; stageDDL.DataBind(); } } } 

ASPX: