Setting the Selectedindex/selectedvalue of a Dropdownlist in a repeater not working
I have a repeater with a DropDownList in it. I set the datasource of this list in the itembound event en set the selectedindex. When I debug the selectedindex is set, but when the page is done loading fo开发者_JS百科r all the item the default item is selected.
This is my code:
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;
}
protected DataTable FilterDropDownData() { var importedFeedColums = I make the table here; DataRow newRow = importedFeedColums.NewRow(); newRow[0] = "Selecteren"; importedFeedColums.Rows.Add(newRow); return importedFeedColums; }
I Als tried to using the Databound even of the dropdown list, but this didnt work either:
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>
When I post the form I can get the selectedvalue and text of each DropDownList. What am I doing wrong
What does FilterDropDownData() return?
Have you tried this?
ddl.Items.FindByValue("Selecteren").Selected = true;
or
ddl.Items.FindByText("Selecteren").Selected = true;
This approach will fail since a drop-down can not have multiple items selected. And setting the "Selected = true" will do exactly the same.
Only way you can set an item as selected is by using the SelectedIndex property of DropDownList.
Ex:ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue("Selecteren"));
精彩评论