views:

6996

answers:

2

i can bind the dropdownlist in the edit item template. The drop down list is having null values.

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");    
  }
}

also getting an error

Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.

+1  A: 

I had problems with find control, in the end I used a little bit of recursion to find the control:

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; 
}

Then to find your control make this call:

drpBuildServers = (DropDownList) FindControlRecursive(e.Row.Cells[0], "ddlBuildServers");
ilivewithian
I tried this but not yet solved.and also when i click the edit button its throwing an errorFailed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request.
Ok, can you provide a short, complete example that demonstrates the problem? If you put the code codefile somewhere I might be able to help.
ilivewithian
A: 
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            
    }
}
bahith