1

I'm developing web-application, in this I want to add all months values on the client side browser in registration form.

I'm doing it manually by adding all months' values one-by-one. I want to know is it possible to add all months with by putting one for loop in my code or not.

Suggest me if it's possible.

Thanks

0

4 Answers 4

5

Here you can do it without the loop:

List<string> months = new List<string>() { "January", "Feburary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
dropDownList1.DataSource = months;
dropDownList1.DataBind();

Or if you insist on using loop:

List<string> months = new List<string>() { "January", "Feburary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
foreach (string month in months)
{
     dropDownList1.Items.Add(month);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Remember to check the IsPostBack condition when using the loop. Not sure how would behave in the first case
1

try with this code

foreach (string name in nameList)
{
    ddl.Items.Add(new ListItem(name));
}

Comments

1

This is one option...

for (int m = 1; m <= 12; m++)
{
   dd.Items.Add(new ListItem(new DateTime(2000, m, 1).ToString("MMMM"), m));
}

UPDATE

I know the question has already been "correctly answered", but taking into account @Jamie's comment, I thought I would add an alternative using the same principle...

DateTime dt = new DateTime(2000, 1, 1);
for (int m = 0; m < 12; m++)
{
   dd.Items.Add(new ListItem(dt.AddMonths(m).ToString("MMMM"), m));
}

3 Comments

It makes more sense to hardcode the value of months and add them as shown in the answer by @Habib.OSU as they will never change (Well, perhaps the name depending on the calendar used). You're creating a new instance of the DateTime object for each iteration.
@Jamie, it makes sense to hard-code if you're not bothered about globalization... I said it was one option, not the only option.
I never said it was the only option either ;). Fair point with globalisation though.
0

Why do it in code at all? Just statically add the months in the aspx file:

<asp:DropDownList Id="ddlMonths" runat="server">
    <asp:ListItem Text="January" Value="1" />
    <asp:ListItem Text="February" Value="2" />
    <asp:ListItem Text="March" Value="3" />
    <asp:ListItem Text="April" Value="4" />
    ...
</asp:DropDownList>

Last time I checked, the months don't change very much! :)

3 Comments

this is only values add one by one ..... I want this same thing via loop - Thanks
Why waste the CPU time adding them via looping when there are only 12? We have a saying in the UK, "Look after the pennies and the pounds will look after themselves" which works just as well for performance. If you are always thinking how to do thing efficiently...
yes, right now there are 12 only...but in programming it makes sense you always using loops..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.