0

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.

Edited by Varunkrishna

4
Contributors
3
Replies
21
Views
4 Years
Discussion Span
Last Post by |-|x
0
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);
            }
        }

Edited by zid8ne

0

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;
        }
    }

Edited by ggamble

1

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.

Edited by |-|x

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.