我有一个Gridview用来显示会员信息,其中数据若干,点击编辑进入EDIT模式用户组这一项会换成DropDwonlist加载数据库里的组数据。问题出来我用
(DropDownList)this.GridView1.Rows[e.NewEditIndex].FindControl("DropDownList1")这种方式查到的值是NULL但是查找Label就可以查到。
请问大侠们如何才能找到这个DropDownList?这个是ASPX里的模板列 <asp:TemplateField HeaderText="用户组">
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server">
                            </asp:DropDownList>
                        </EditItemTemplate>
                        <HeaderStyle BorderStyle="None" HorizontalAlign="Left" />
                        <ItemTemplate>
                            <asp:Label ID="lbgroup" runat="server" Text='<%# getgroup( Eval("m_group").ToString()) %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
下面是ROWEDIT事件的代码    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        SqlConnection con = Common.createCon();
        con.Open();
        SqlCommand cmd = new SqlCommand("select g_title,g_id from " + Common.tabpre + "group", con);
        SqlDataReader sda = cmd.ExecuteReader();
        string groupid;
        while (sda.Read())
        {
           groupid=((Label)this.GridView1.Rows[e.NewEditIndex].FindControl("lbgroup")).Text;
           int i=0;
           DropDownList ddl = ((DropDownList)this.GridView1.Rows[e.NewEditIndex].FindControl("DropDownList1"));
           ddl.Items.Add(new ListItem(sda["g_title"].ToString(), sda["g_id"].ToString()));
           if (groupid == sda["g_title"].ToString())
           {
               ((DropDownList)this.GridView1.Rows[e.NewEditIndex].FindControl("ddlgroup")).SelectedIndex = i;
           }
           i++;
        }
        sda.Close();
        con.Close();
        this.GridView1.EditIndex = e.NewEditIndex;
        loaddata();   
    }DropDownList ddl = ((DropDownList)this.GridView1.Rows[e.NewEditIndex].FindControl("DropDownList1"));
问题就出在这一句上,返回的控件是NULL

解决方案 »

  1.   

    在RowEditing的时候DropDownList还没有在页面生成,应该在RowDataBound把数据绑定到DropDownListprotected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowState == DataControlRowState.Edit)
        {
            DropDownList ddl = ((DropDownList)e.Row.FindControl("DropDownList1"));
            ddl.Items.Add(new ListItem(...));
        }
    }
      

  2.   

    感谢ldarmy,问题已解决,不过论坛说5天后才能加分......