SelectedValue DropDownList in Repeater

How to set the selected dropDownList element inside the repeater?

The repeater is bound to the resulting DataTable, and dropDownList is bound to the DropDownList DataTable in the code behind. I need to set the SelectedValue property in DropDownList to the field value from the repeater table.

Here is what I tried:

<asp:Repeater runat="server" ID="myRepeater> <ItemTemplate> <asp:DropDownList runat="server" CssClass="fullSelect" ID="degree_dropdown" AppendDataBoundItems="true" selectedValue='<%#DataBinder.Eval(Container.DataItem,"degreeCode")%>'> <asp:ListItem Text="Select Degree" /> </asp:DropDownList> </ItemTemplate> </asp:Repeater> 

Code to fill the repeater:

 myRepeater.DataSource = myRepeaterData; //myRepeaterData is a datatable myRepeater.DataBind(); 

Code to populate the drop-down list:

 protected void educationPopup_repeater_ItemDataBound(object sender, RepeaterItemEventArgs e) { DropDownList degree_dropdown = e.Item.FindControl("degree_dropdown") as DropDownList; if (degree_dropdown != null) { degree_dropdown.DataSource = degrees; //a datatable degree_dropdown.DataTextField = "degree"; degree_dropdown.DataValueField = "code"; degree_dropdown.DataBind(); } } 
+8
data-binding repeater
source share
2 answers

You are almost there. You just need to drop the DataItem to a DataRowView and assign it a DropDownList as follows:

 protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { var degree_dropdown = e.Item.FindControl("degree_dropdown") as DropDownList; string degreeCode = (string) ((DataRowView) e.Item.DataItem)["degreeCode"]; if (degree_dropdown != null) { degree_dropdown.DataSource = degrees; //a datatable degree_dropdown.DataTextField = "degree"; degree_dropdown.DataValueField = "code"; degree_dropdown.DataBind(); if (degree_dropdown.Items.FindByValue(degreeCode) != null) degree_dropdown.SelectedValue = degreeCode; } } } 
+7
source share

With custom HTML5 attributes, you can set the value of the drop-down list to the data attribute, and then set it as the selected value after the drop-down list with data binding, I linked the drop-down menu using asp: ObjectDataSource

 <asp:Repeater runat="server" ID="myRepeater> <ItemTemplate> <asp:DropDownList runat="server" CssClass="fullSelect" ID="degree_dropdown" AppendDataBoundItems="true" SetValue='<%#DataBinder.Eval(Container.DataItem,"degreeCode")%>' datasourceid="dsCategory" datatextfield="degree" datavaluefield="code" onprerender="DropDownDataBinding"> <asp:ListItem Text="Select Degree" /> </asp:DropDownList> <asp:ObjectDataSource ID="dsCategory" runat="server" SelectMethod="LoadDegree" TypeName="WebApplication.WebForm1" /> </ItemTemplate> </asp:Repeater> 

CodeBehind

 protected void DropDownDataBinding(object sender, EventArgs e) //Method to set the selected value on Category dropdown inside repeater { DropDownList sel = (DropDownList)sender; sel.Value = sel.Attributes["SetValue"]; ListItem li = new ListItem("<< Select >>", ""); sel.Items.Insert(0,li); } protected DataTable LoadDegree() { DataTable dt = new DataTable(); dt = degrees; //a datatable return dt; } 

Linking your repeater will remain the same

0
source share

All Articles