Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SelectedValue of DropDownList in Repeater

How do I set the selected item of a dropDownList inside a repeater?

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

This is what I've 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 populate repeater:

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

Code to populate dropdownlist:

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();
            }
}
like image 964
dmr Avatar asked May 28 '13 20:05

dmr


People also ask

How to Find DropDownList in Repeater in ASP net?

DataSource =dt; RepDetails. DataBind(); } protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e) { DropDownList ddlPageSize = (DropDownList)sender; // here rebind the repeater control by setting the page index. }

Which control is used for a DropDownList?

Certify and Increase Opportunity. The DropDownList Web server control allows users to select an item from a predefined list. It differs from the ListBox Web server control in that the list of items remains hidden until users click the drop-down button.

How do I add a default item to DropDownList?

The default value of the select element can be set by using the 'selected' attribute on the required option. This is a boolean attribute. The option that is having the 'selected' attribute will be displayed by default on the dropdown list.


1 Answers

You are almost there. You just need to cast DataItem to DataRowView, and assign it to DropDownList like this -

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;
        }
    }
}
like image 68
Win Avatar answered Oct 19 '22 07:10

Win