Repeater中DropDownList的SelectedValue

时间:2013-05-28 20:56:15

标签: asp.net data-binding repeater asp.net-3.5 selectedvalue

如何在转发器中设置dropDownList的选定项?

转发器绑定到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();
            }
}

2 个答案:

答案 0 :(得分:7)

你快到了。您只需将DataItem转换为DataRowView,并将其分配给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;
        }
    }
}

答案 1 :(得分:0)

使用HTML5 custom attributes,您可以将下拉列值设置为数据属性,然后在下拉列表进行数据绑定后将其设置为选定值。我使用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;

}

您的转发器控件的绑定将保持不变