Filling DropDownList inside Repeater not working

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

Here is 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 on the right path? :)

+4
source share
6 answers

Adjust other answers on this question, the ItemDataBound event should not be used to bind management data, do it at the management level.

In the drop-down list, specify the data binding event:

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

Then fire 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 bind everything at the management level, and not look for things and have your own grid in order to know what it contains. Each control can take care of itself;) Thus, it is much easier to add and remove controls in your template and isolate these changes.

+21
source
 for(int i=1;i > 5;i++) 

Must read ...

 for(int i=1;i < 5 ;i++) 
+6
source

On 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())); } } } 
+1
source

private void criteriaScore_ItemDataBound (object source, RepeaterCommandEventArgs e)

0
source

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

0
source

Edit: for (int i = 1; i> 5; i ++) To: for (int i = 1; i <5; i ++)

Or:

  using System.Linq; ddl.DataSource = Enumerable.Range(1, 5); ddl.DataBind(); ddl.SelectedValue = yourValue 
0
source

All Articles