Renju
Renju

Reputation:

Binding dropdown list in a gridview edit item template

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.

Upvotes: 1

Views: 26433

Answers (4)

Test
Test

Reputation: 1

Its a solution for me:

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

Upvotes: 0

liliane
liliane

Reputation: 21

try http://www.codeproject.com/KB/webforms/editable_gridview_control.aspx , it could be helpful

Upvotes: 1

bahith
bahith

Reputation: 441

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

Upvotes: 1

ilivewithian
ilivewithian

Reputation: 19702

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

Upvotes: 1

Related Questions