前台:  
                             [align=left]   <asp:TemplateColumn HeaderText="所属部门">
                                      <ItemTemplate>
                                          <%# DataBinder.Eval(Container.DataItem, "dept_name")%>
                                      </ItemTemplate>
    <EditItemTemplate>
                                        <asp:DropDownList ID="ddl_dept" runat="server">
                                        </asp:DropDownList>
                                    </EditItemTemplate>
                                    </asp:TemplateColumn>[/align]我在编辑的时候,怎么才能绑定到此DropDownList上呢?(内容是从数据库中读出后绑定到此,是无限级分类)后台:在点击DataGrid的编辑命令里面加入以下内容
 this.ddl_dept.Items.Insert(0, new ListItem("==请选择部门==", "0"));提示:未将对象引用设置到对象的实例。 请问我这样绑定的思路对不??

解决方案 »

  1.   

    this.ddl_dept当然不行。要先在DataGrid里FindControl,再进行赋值。
      

  2.   


    能再具体点吗? 我用FindControl是得到某列的值,但我怎么得到这个DropDownList控件呢? 谢谢了
      

  3.   

    在GridView的
      protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            { 
         if (e.Row.RowType == DataControlRowType.DataRow)
                {
              DropDownList dr= (DropDownList)e.Row.FindControl("ddl_dept"))
                
                dr.DataBind()  
                .........            
               } 
            }
      

  4.   

    e.Item.FindControl("ddl_dept") 这样写吗? 可我还是不知道怎么得到这个控件
      

  5.   

    4楼说的是在绑定事件里查找。你在编辑事件里也同样可以。如果知道行,也可以
    DropDownList DDL= (DropDownList)DataGrid1.Rows[行].FindControl("ddl_dept"));再对DDL进行数据绑定。
      

  6.   

    没用过DataGrid,在Repeater里面的,你参考下吧
    protected void Repeater1_ItemDataBound1(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DropDownList ddl= (DropDownList )e.Item.FindControl("ddl_dept");
            }
        }
      

  7.   

     DropDownList ddl =((DropDownList)GridView1.Rows[0].Cells[1].FindControl("ddl_dept"))  ddl.DataSource = ...
      ddl.DataTextField = ..
      ddl.DataBind();
      

  8.   

    DropDownList dr = (DropDownList)e.Item.FindControl("ddl_dept");
    dr.Items.Insert(0, new ListItem("==请选择部门==", "0"));还是提示:未将对象引用设置到对象的实例晕~~~
      

  9.   

    你要先生成GridView,然后在引起回发的点击或编辑事件里去找到并转换控件
      

  10.   

     SqlConnection mySqlConn = new SqlConnection(sqlConn);
            string sql = "select id,d_name from dept_name where pid=@pid and del_sign=0 order by pid";
            SqlCommand cmd = new SqlCommand(sql, mySqlConn);
            SqlParameter Pid = new SqlParameter("@pid", SqlDbType.Int);
            Pid.Value = pid;
            cmd.Parameters.Add(Pid);
            mySqlConn.Open();
            SqlDataReader sdr = cmd.ExecuteReader();
            while (sdr.Read())
            {
                this.DropDownList1.Items.Add(new ListItem(t_sign + " " + sdr[1].ToString(), sdr[0].ToString()));
                t_sign += "─┴";
                this.GetArticleCategory(sdr[0].ToString());
                t_sign = t_sign.Substring(0, t_sign.Length - 2); 
            }        sdr.Close();
            mySqlConn.Close();我平时绑定DropDownList是这样绑定的。
      

  11.   


    我是在点击DataGrid的编辑命令(即:OnEditCommand)时去找此控件的,不知道这样对不?
      

  12.   

    无限级分类的数据你一个dll怎么可能显示!得动态生成dll吧!~
      

  13.   


    protected void dgCTSpeak_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
        if(e.Item.ItemType == ListItemType.EditItem)
        {
            DropDownList editCTSpeakFromHour = e.Item.Cells[5].FindControl("editCTSpeakFromHour") as DropDownList;
            //...
        }
    }
      

  14.   

    void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.EditItem)
        {
            DropDownList dp= (DropDownList)e.Item.FindControl("ddl_dept"));
            ......    }
    }
      

  15.   

    void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.EditItem)
        {
            DropDownList dp= (DropDownList)e.Item.FindControl("ddl_dept"));
            ......    }
    }
      

  16.   


    现在可以初始化DropDownList了,但我绑定的时候是调用 的一个函数。即:     if (e.Item.ItemType == ListItemType.EditItem)
            {
                DropDownList dr = (DropDownList)e.Item.FindControl("ddl_dept");
                dr.Items.Insert(0, new ListItem("==请选择部门==", "0"));
                Get_List("0");
            }private void Get_List(string pid)
        {
            SqlConnection mySqlConn = new SqlConnection(sqlConn);
            string sql = "select id,d_name from dept_name where pid=@pid and del_sign=0 order by pid";
            SqlCommand cmd = new SqlCommand(sql, mySqlConn);
            SqlParameter Pid = new SqlParameter("@pid", SqlDbType.Int);
            Pid.Value = pid;
            cmd.Parameters.Add(Pid);
            mySqlConn.Open();
            SqlDataReader sdr = cmd.ExecuteReader();
            while (sdr.Read())
            {
                dr.Items.Add(new ListItem(t_sign + " " + sdr[1].ToString(), sdr[0].ToString()));
                t_sign += "─┴";
                this.Get_List(sdr[0].ToString());
                t_sign = t_sign.Substring(0, toadd.Length - 2);
            }        sdr.Close();
            mySqlConn.Close();
        }在Get_List函数中不能直接用dr
      

  17.   

    自己顶一下~通过判断 edit命令后找到该控件,并命名为"dr"这个控件可以用到调用的函数中吗?