我无法更改当前学年的选定值 我使用下面的代码:
Asp.net
<table>
<tr>
<td>
<b><asp:Label ID="lblSelectAcYear" runat="server" Text="Select AcYear"></asp:Label></b>
</td>
<td>
<asp:DropDownList ID="ddlAcYear" runat="server" OnDataBinding="ddlBinding" OnDataBound="ddlAcYear_DataBound" AutoPostBack="true" Width="150px" OnSelectedIndexChanged="ddlAcYear_SelectedIndexChanged">
</asp:DropDownList>
</td>
<td><b><asp:Label ID="lblSelectSem" runat="server" Text="Select Semester"></asp:Label></b></td>
<td>
<asp:RadioButtonList ID="rblSelectSem" runat="server" AutoPostBack="true" RepeatDirection="Horizontal">
<asp:ListItem Enabled="true" Selected="True" Text="Even"></asp:ListItem>
<asp:ListItem Enabled="true" Text="Odd"></asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
</table>
和背后的代码
ddlAcYear.DataSource = Res.AllData;
ddlAcYear.DataValueField = "AcYearId";
ddlAcYear.DataTextField = "AcYear";
ddlAcYear.DataBind();
foreach (DataRow dr in Res.AllData.Rows)
{
if ((DateTime.Now.Date > Convert.ToDateTime(dr.ItemArray[2]).Date && DateTime.Now.Date < Convert.ToDateTime(dr.ItemArray[3]).Date))
{
ddlAcYear.SelectedValue = dr.ItemArray[0].ToString();
}
}
当我调试时,所选值显示正确,但是当显示页面时,会显示下拉列表中的第一项。 :'(
答案 0 :(得分:0)
这是我总是在下拉列表中选择项目的方式:
ListItem liId = ddlAcYear.Items.FindByValue(dr.ItemArray[0].ToString());
if (liId != null) {
liId.Selected = true;
}