8

如何在中继器内设置 dropDownList 的选定项?

在后面的代码中,repeater绑定到repeaterData DataTable,dropDownList绑定到dropDownList DataTable。我需要将 DropDownList 的 SelectedValue 属性设置为 repeaterData 表中字段的值。

这是我尝试过的:

<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>

填充中继器的代码:

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

填充下拉列表的代码:

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();
            }
}
4

2 回答 2

9

你快到了。您只需要转换DataItemDataRowView,并将其分配给DropDownList这样的 -

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;
        }
    }
}
于 2013-05-28T22:24:58.113 回答
0

使用HTML5 自定义属性,您可以将下拉值设置为数据属性,然后在下拉数据绑定后将其设置为选定值。我已经使用 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>

代码隐藏

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;

}

您的中继器控件的绑定将保持不变

于 2015-05-24T12:30:12.763 回答