This is a migrated thread and some comments may be shown as answers.

DropDownList not being found

2 Answers 87 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kyle
Top achievements
Rank 1
Kyle asked on 08 Mar 2012, 10:15 PM
Hello all,

I'm trying to put a DropDownList in my EditForm, and populate it in the ItemCommand method. Existing codebehind is like this:

GridEditFormItem item = (GridEditFormItem)(e.Item as GridDataItem).EditFormItem;
                DropDownList ddlTeams = (DropDownList)item.FindControl("ddlTeams");
                ddlTeams.DataSource = GetTeamsDS();
                ddlTeams.DataTextField = "TeamName";
                ddlTeams.DataValueField = "TeamName";
                ddlTeams.DataBind();


My problem is that the FindControl() method isn't actually finding the control. I'm 100% sure everything is named the same, and I've used this method on other types of controls (listBox, Label, Button, etc.) but when I'm using it on the DDL it just fails. Should I be doing this in another method? or is there a way to make it work here? 

2 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 09 Mar 2012, 05:31 AM
Hello,

Try populate the DropDownList in ItemDataBound event.
C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
  if (e.Item is GridEditableItem && e.Item.IsInEditMode)
  {
    GridEditableItem item = (GridEditableItem)e.Item;
    DropDownList ddlTeams = (DropDownList)item.FindControl("ddlTeams");
    //populate the DropDownList here
  }
}

-Shinu.
0
Kyle
Top achievements
Rank 1
answered on 09 Mar 2012, 03:30 PM
this works. Thank you very much!
Tags
Grid
Asked by
Kyle
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Kyle
Top achievements
Rank 1
Share this question
or