views:

637

answers:

5

I'm trying to populate a dropdown list inside a repeater, but I'm not being very successful. I'm probably using the wrong EventArgs e.

Here's my aspx code:

        <asp:Repeater runat="server" id="criteriaScore">
            <HeaderTemplate>
              <ul>         
                <li class="header"><span class="item">Kriterie</span><span class="value">Poeng</span><span class="description">Beskrivelse</span></li>
            </HeaderTemplate>
            <ItemTemplate>
                <li>
                    <span class="item"> <%# Eval("criteria") %>:</span>
                    <asp:DropDownList id="ddlRating" runat="server" autopostback="true" enableviewstate="false"></asp:DropDownList>
                    <span class="value score<%# Eval("lvl") %>" title="<%# Eval("description") %>"> </span>
                </li>
            </ItemTemplate>
            <FooterTemplate>
                </ul>  
            </FooterTemplate>
        </asp:Repeater>

And the code behind:

    protected void criteriaScore_ItemDataBound(object sender, DataListCommandEventArgs e)
    {
        DropDownList ddl = (DropDownList)e.Item.FindControl("ddlRating");

        for(int i=1; i > 5; i++)
        {
            ddl.Items.Add(new ListItem(i.ToString(), i.ToString()));
        }
    }

Can someone please guide me down the right path? :)

A: 

private void criteriaScore_ItemDataBound(object source, RepeaterCommandEventArgs e)

twlichty
+5  A: 
for(int i=1;i > 5;i++)

Should read ...

for(int i=1;i < 5 ;i++)
bleeeah
+1, moreover it is normally considered good practice to start iterating with `i=0`
Juri
+1  A: 

In the .aspx Page:

<asp:Repeater runat="server" id="criteriaScore"   
              OnItemDataBound="criteriaScore_ItemDataBound">

In the Code-Behind:

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

         for(int i=0; i < 5; i++)
         {
            ddl.Items.Add(new ListItem(i.ToString(), i.ToString()));
         }
    }
 }
rick schott
Thanks, that did the trick :)
Steven
A: 

Regardless of how the method is implemented (there are a few ways), the ItemDataBound event is not attached to the repeater in the markup.

Jon Seigel
+3  A: 

Depsite the other answers on this question, the ItemDataBound event should not be used to bind control data, do that at the control level.

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 < 5; i++)
    {
         ddl.Items.Add(new ListItem(i.ToString(), i.ToString()));
    }

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

You should try and do all 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 ;) This way it is much easier to add and remove controls to your template and keep these changes isolated.

Kelsey
Thanks. The above answer works, but since this is better practice, I will go for this solution :)
Steven
+1 simple nice answer
Amr ElGarhy