Asp.net 中继器中DropDownList的SelectedValue

Asp.net 中继器中DropDownList的SelectedValue,asp.net,data-binding,repeater,asp.net-3.5,selectedvalue,Asp.net,Data Binding,Repeater,Asp.net 3.5,Selectedvalue,如何在中继器内设置dropDownList的选定项 中继器绑定到repeaterData数据表,dropDownList绑定到代码隐藏中的dropDownList数据表。我需要将DropDownList的SelectedValue属性设置为repeaterData表中字段的值 这就是我尝试过的: <asp:Repeater runat="server" ID="myRepeater> <ItemTemplate> <asp:DropDownList runa

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

中继器绑定到repeaterData数据表,dropDownList绑定到代码隐藏中的dropDownList数据表。我需要将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>
填充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();
            }
}

你就快到了。您只需将
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;
        }
    }
}
使用,可以将下拉列表值设置为数据属性,然后在下拉列表数据索引后将其设置为选定值。我已经使用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>
,”);
选择项目。插入(0,li);
}
受保护的DataTable LoadDegree()
{
DataTable dt=新的DataTable();
dt=度;//数据表
返回dt;
}

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

您使用什么事件或方法来填充
degree\u下拉列表
?您可以发布代码吗?
<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;

}