views:

987

answers:

2

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.

A: 

I've had trouble with that in the past. I believe the way I approached this was to find the item in the dropdown list and set the selected property to true.

EDIT: Here are two examples - I prefer the FindByValue syntax myself...

YourDDL.ClearSelection();
foreach (ListItem item in YourDDL.Items)
{
   if (item.Value == YourString) 
   { 
      item.Selected = true;
      break;
   }
}


YourDDL.Items.FindByValue("1").Selected = true;
Mayo
+4  A: 

Whoa you are making this way more complicated than it needs to be. Don't do this on the ItemDataBound, do it on the controls DataBinding property.

In your dropdownlist define the databinding event:

<asp:DropDownList runat="server" ID="ddlYourDDL" OnDataBinding="ddlYourDDL_DataBinding">

Then implement the OnDataBinding event:

protected void ddlYourDDL_DataBinding(object sender, System.EventArgs e)
{
    DropDownList ddl = (DropDownList)(sender);
    for (int i = 1; i < 6; i++)
    {
        ddl.Items.Add(new ListItem(i.ToString(), i.ToString()));
    }

    // Now that the items are all there, set the selected property
    ddl.SelectedValue = Eval("lvl").ToString();
}

You should try and do your databinding at the control level instead of searching for things and having your grid have to know about what it contains. Each control can take care of itself ;)

I disagree with that answer given in your previous linked question, to use the ItemBound event. This event should be used at 'item' level when something needs to affect more than a single control.

Edit: I have also added this info to your other question so if it is found in the future, others do no use the ItemBound event for this purpose.

Kelsey
Yup, that worked pretty nicely :)
Steven