This is a followup from http://stackoverflow.com/questions/1432790/populating-dropdownlist-inside-repeater-not-working.
I'm adding a dropdownlist inside a repeater. Now I need to set the selected value, but that easier said than done...
Here is my code:
protected void criteriaScore_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
// This event is raised for the header, the footer, separators, and items.
// Execute the following logic for Items and Alternating Items.
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DropDownList ddl = (DropDownList)e.Item.FindControl("ddlRating");
DataRowView drw = (DataRowView)e.Item.DataItem;
for (int i = 1; i < 6; i++)
{
ddl.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
ddl.DataBind(); // <- Not sure if this is needed here.
ddl.SelectedValue = drw.Row["lvl"].ToString();
}
}
drw.Row["lvl"].ToString() is a value from 1 -5.
I've tried setting SelectedValue and SelectedIndex.
I also tried
ddl.SelectedValue = Convert.ToString(ddl.Items.FindByText(drw.Row["lvl"].ToString()).Value) ;
I'm running out of ideas.