asp.net – 在转发器中设置Dropdownlist的Selectedindex / selectedvalue不起作用

我有一个带DropDownList的转发器.我在itembound事件中设置了这个列表的数据源,并设置了selectedindex.当我调试selectedindex被设置,但当页面完成加载所有项目时,选择默认项目.

这是我的代码:

 protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        var ddl = (DropDownList)e.Item.FindControl("DataFeedItems");
        ddl.DataSource = FilterDropDownData();
        ddl.DataTextField = "ColumnName";
        ddl.DataValueField = "ColumnName";
        ddl.DataBind();
        ddl.SelectedValue = "Select";
        ddl.SelectedIndex = 28;
    }

06001

我Als尝试使用Databound甚至下拉列表,但这也没有用:

    protected void DataFeedItems_DataBound(object sender, System.EventArgs e)
    {
        var ddl = (DropDownList) (sender);
        ddl.SelectedValue = "Selecteren";
        ddl.Items[28].Selected = true;

    }

 <asp:Repeater ID="Repeater1" runat="server" 
            onitemdatabound="Repeater1_ItemDataBound">
            <ItemTemplate>
                <li>
                <label><%# DataBinder.Eval(Container.DataItem, "ColumnName") %></label>                       
                    <asp:DropDownList ID="DataFeedItems" ClientIDMode="Static" runat="server" DataSource='<%# FilterDropDownData() %>'
                        DataTextField="ColumnName" DataValueField="ColumnName"   OnDataBound="DataFeedItems_DataBound" >
                    </asp:DropDownList>
                    <input id="Hidden1" runat="server" clientidmode="Static" type="hidden" value='<%# DataBinder.Eval(Container.DataItem, "ColumnName") %>' />
                </li>
            </ItemTemplate>
        </asp:Repeater>

当我发布表单时,我可以获得每个DropDownList的selectedvalue和text.我究竟做错了什么

最佳答案 FilterDropDownData()返回什么?

你试过这个吗?

 ddl.Items.FindByValue("Selecteren").Selected = true;

要么

 ddl.Items.FindByText("Selecteren").Selected = true;
点赞