user59637
user59637

Reputation: 85

Need help in dropdownlist control

I have ridiculous problem with dropdownlist.Through for loop i have inserted dropdown value for Day,Month and Year. But when i am submitting the form after selecting the Day,Month and Year from Dropdownlist,the list item value is reindexing after each submitting.Ex.For Day 1 to 31,Month 1 to 12, year 1970 to 2009.After Submitting the form the list item is becoming double for each dropdownlist.Like in day dropdownlist before submiting it was 1 to 31 after submitting if i check dropdownlist the value will be two times 1 to 31 I mean 1 to 31 again 1 to 31 inside the listitem it happens for each dropdownlist.Here is my code: 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;

Thanks, Sumit

Upvotes: 0

Views: 397

Answers (1)

Raj
Raj

Reputation: 6830

you probably are not checking for Page.IsPostBack option

if it is true, then DO NOT REPOPULATE your drop down lists

Upvotes: 1

Related Questions