在gridview编辑项模板中绑定下拉列表

时间:2009-03-07 08:29:33

标签: c# .net asp.net gridview

我可以在编辑项目模板中绑定下拉列表。下拉列表具有空值。

protected void grdDevelopment_RowDataBound(object sender, GridViewRowEventArgs e) 
{   
  DropDownList drpBuildServers = new DropDownList();

  if (grdDevelopment.EditIndex == e.Row.RowIndex)    
  {        
      drpBuildServers = (DropDownList)e.Row.Cells[0].FindControl("ddlBuildServers");    
  }
}

也出错了

无法加载视图状态。正在加载视图状态的控制树必须与在上一个请求期间用于保存视图状态的控制树匹配。例如,在动态添加控件时,在回发期间添加的控件必须与初始请求期间添加的控件的类型和位置相匹配。

4 个答案:

答案 0 :(得分:1)

我遇到了find控件的问题,最后我用了一点点递归来找到控件:

private Control FindControlRecursive(Control root, string id) 
{ 
     if (root.ID == id)
    { 
         return root; 
    } 

    foreach (Control c in root.Controls) 
    { 
         Control t = FindControlRecursive(c, id); 
        if (t != null) 
        { 
            return t; 
        } 
    } 

     return null; 
}

然后找到你的控件进行这个调用:

drpBuildServers = (DropDownList) FindControlRecursive(e.Row.Cells[0], "ddlBuildServers");

答案 1 :(得分:1)

protected void grdDevelopment_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DropDownList drpBuildServers;

        drpBuildServers = e.Row.FindControl("ddlBuildServers") as DropDownList;

        if (drpBuildServers != null)
            // Write your code here            
    }
}

答案 2 :(得分:1)

答案 3 :(得分:0)

它是我的解决方案:

protected void grdDevelopment_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DropDownList drpBuildServers;

        drpBuildServers = e.Row.FindControl("ddlBuildServers") as DropDownList;

        if (drpBuildServers != null)
            // Write your code here            
    }
}