I had created a dropdownlist box as follows. The contents of default.aspx is listed below

<asp:DropDownList runat="server" ID="day" CssClass="dobselect">
   <asp:ListItem Value="0" Text="DAY"></asp:ListItem>
 </asp:DropDownList>
 <asp:DropDownList runat="server" ID="month" CssClass="dobselect">
     <asp:ListItem Value="0" Text="MONTH"></asp:ListItem>
 </asp:DropDownList>
 <asp:DropDownList ID="year" CssClass="dobselect" runat="server">
     <asp:ListItem Value="0" Text="YEAR"></asp:ListItem>
 </asp:DropDownList>

Then in the default.cs.aspx I had added the following code to display the dropdownlist boxe's values

 private void autofilldate(int incrementBy, int max, int itemcount)//to automatically generate days in a dropdownlist
 {
   int incr = incrementBy;
   for (int i = 0; i <itemcount; i++)
   {
     if (incrementBy <= max)
      {
        day.Items.Add(new ListItem(incrementBy.ToString(), incrementBy.ToString()));

      }
    }
}//end of autofilldl
// ....more code for displaying month and year 

private void autofillyr(int incrementBy, int maxyr, int no_year)
    {
       int yr = incrementBy;
       for (int i =0; i < no_year; i++)
       {
           if (incrementBy <= maxyr)
           {
               year.Items.Add(new ListItem(incrementBy.ToString(), incrementBy.ToString()));
               incrementBy =incrementBy + yr;
           }

protected void Page_Load(object sender, EventArgs e)
    {
        autofilldate(1, 31, 31);
        autofillmonth(1, 12, 12);
        autofillyr(1, 2050, 2076);
    }

when it is executed both the day and month is working fine, but for the year it is displaying as 1 2 3..... 2050 and so on. what I need is it must start from the year"1900" it should not get displayed from the value 1 to 2050. How can I do this? anyone please help me.

ab.AutoFillYr(2000, 20);

        public void AutoFillYr(int startYear, int noYear)
        {
            List<int> list = new List<int>();

            for (int i = 0; i < noYear; i++)
            {
                int y = startYear;
                int z = y + 1;
                startYear = z;
                list.Add(z);
            }
        }

Line 15 - 24

    private void autofillyr(int incrementBy, int maxyr, int no_year)
    {
        for (int i = startYear; i < maxyr; i++)
        {
            if(i != no_year)
                year.Items.Add(new ListItem(i.ToString(), i.ToString()));
            i = i + incrementBy;
        }
    }

You can do it all in the loop if you handle the parameters properly.

private void autofillyr(int incrementBy, int maxyr, int no_year)
{
   for (int i = no_year; i < maxyr; i += incrementBy)
   {
       year.Items.Add(new ListItem(i.ToString(), i.ToString()));
   }
}

and the function call...

protected void Page_Load(object sender, EventArgs e)
{
    ...
    autofillyr(1, 2050, 1900);
}

actually, i repurposed your no_year parameter as start year since it is unclear what the purpose of this parameter is.

I would susggest doing the days function the same way since

if (incrementBy <= max) // this line is irrelevant

try...

private void autofilldate(int incrementBy, int max) //don't need `itemcount`
{
    for (int i = 1; i < max; i += incrementBy)
    {
        day.Items.Add(new ListItem(i.ToString(), i.ToString()));
    }
}

You haven't posted your month function, but it can be done the same way. In fact you could use the same function to fill all 3 dropdowns (as per the Year function I just posted) call them with appropriate start, max, and increment values, just pass the dropdown itself as another parameter. I would also make the increment an optional parameter with default of 1.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.