• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 501
  • Last Modified:

Easy: Populate 2 Drop Down Lists with Date Ranges - Month, Year

I'm trying to populate these controls on page load. Month = 1-12, Year = Current Year + 20

<asp:DropDownList ID="lstMonth" runat="server"></asp:DropDownList>
<asp:DropDownList ID="lstYear" runat="server"></asp:DropDownList>

protected void Page_Load(object sender, EventArgs e)
    {
        ArrayList arMonth = new ArrayList();
        for (int i = 1; i < 13; i++)
        {
            arMonth.Add(i);
        }
        lstMonth.DataSource = arMonth;
        lstMonth.DataBind();
        lstMonth.Items.
    }
 
or
 
    protected void Page_Load(object sender, EventArgs e)
    {
 
        for (int i = 1; i < 13; i++)
        {
            String iVal = System.Convert.ToString(i);
            ListItem li = new ListItem();
            li.Text = iVal;
            li.Value = iVal;
            lstMonth.Items.Add(li);
        }
    }
 
both didn't work

Open in new window

0
coldchillin
Asked:
coldchillin
  • 5
2 Solutions
 
tempstf1Commented:
I just tested out your second method and it worked for me.. do not know what to tell you :s
0
 
coldchillinAuthor Commented:
Is this what your control looks like?

<asp:DropDownList ID="lstMonth" runat="server"></asp:DropDownList>
0
 
coldchillinAuthor Commented:
When I use:
    protected void Page_Load(object sender, EventArgs e)
    {
 
        for (int i = 1; i < 13; i++)
        {
            String iVal = System.Convert.ToString(i);
            ListItem li = new ListItem();
            li.Text = iVal;
            li.Value = iVal;
            lstMonth.Items.Add(li);
        }
    }

I have the box, and it has the appropriate length as if it was filled, but there are no displayed/clickable values...
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
coldchillinAuthor Commented:
I also added the code from:
http://www.aspnettutorials.com/tutorials/controls/dropdownlist-binddate-csharp.aspx

and all my listboxes filled empty?! what's going on? I can statically set the listitem, that's the only way I'm getting this to populate...
0
 
coldchillinAuthor Commented:
Even this gives me nothing.

for (int i = 1; i < 13; i++)
        {
            lstMonth.Items.Add("yo");
        }

Here is the .aspx code:
<asp:DropDownList ID="lstMonth" runat="server" ForeColor="#400000">
                    </asp:DropDownList>

what am i missing?!
0
 
coldchillinAuthor Commented:
Here's what happened. The site is running a Master Page which was overriding everything in the Page Load event!
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now