【问题标题】:Need help in dropdownlist control在下拉列表控件中需要帮助
【发布时间】: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


【解决方案1】:

您可能没有检查 Page.IsPostBack 选项

如果是真的,那么不要重新填充你的下拉列表

【讨论】:

  • 老兄,感谢还不够。如果它解决了您的问题,您需要为我的答案投票并接受它作为正确答案
猜你喜欢
  • 1970-01-01
  • 2017-07-09
  • 2023-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多