【发布时间】:2009-04-06 14:33:28
【问题描述】:
我对下拉列表有可笑的问题。通过 for 循环,我插入了日、月和年的下拉值。但是当我从下拉列表中选择日、月和年后提交表单时,列表项值在每次提交后重新索引。例如,1970 年到 2009 年的第 1 天到第 31 天,第 1 到 12 个月。提交表单后每个下拉列表的列表项都变为双倍。就像在提交之前的天下拉列表中一样,如果我检查下拉列表,则在提交后它是 1 到 31,该值将是 1 到 31 的两倍,我的意思是 1 到 31 再次在列表项中发生 1 到 31对于每个下拉列表。这是我的代码: aspx:
<asp:DropDownList ID="DropDownDay" runat="server">
<asp:ListItem Text="Select Day"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownMonth" runat="server">
<asp:ListItem Text="Select Month"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownYear" runat="server">
<asp:ListItem Text="Select Year"></asp:ListItem>
</asp:DropDownList>
aspx.cs:
//Inserting day in the day dropdown list.
for (int i = 1; i <=31; i++)
{
ListItem item = new ListItem();
item.Value = i.ToString();
DropDownDay.Items.Add(item.Value);
}
//Inserting month in the month dropdown list.
for (int i = 1; i <=12; i++)
{
ListItem item = new ListItem();
item.Value = i.ToString();
DropDownMonth.Items.Add(item.Value);
}
//Inserting year in the year dropdown list.
for (int i = 1970; i <=2009; i++)
{
ListItem item = new ListItem();
item.Value = i.ToString();
DropDownYear.Items.Add(item.Value);
}
string day = DropDownDay.Text;
string month = DropDownMonth.Text;
string year = DropDownYear.Text;
After adding i have set:
DropDownDay.SelectedIndex = 0;
DropDownMonth.SelectedIndex = 0;
DropDownYear.SelectedIndex = 0;
谢谢, 苏米特
【问题讨论】:
-
使用代码示例格式化按钮(看起来像:'101010')来格式化您的代码。这样的无格式博客很难阅读。
标签: drop-down-menu