What is the best way to code up a Month and Year d

2019-01-24 06:05发布

I have an internal application that I needs to have a drop down list for two date type elements: Month and Year. These values are not in a database or other repository of information.

I know I could just setup a list with the values I need by adding them to a dictionary like object (I need to correlate the Month to the numerical representation, January => 01):

var months = new Dictionary<String,String>();
months.Add("01", "January");
...

The drop down list for the year will be a bit easier as I can just choose a starting year and iterate up to the current or current+1 year in a generic list.

Is there a better way to handle these data elements? Something built in, or a good design pattern that I should be implementing?

7条回答
forever°为你锁心
2楼-- · 2019-01-24 06:56

Below code is for load month dropdown as Select

    private void LoadMonth()
    {
        ddlmonth.Items.Add(new ListItem("Select", 0.ToString()));
        var months = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames;
        for (int i = 0; i < months.Length-1; i++)
        {
            ddlmonth.Items.Add(new ListItem(months[i], (i+1).ToString()));
        }
    }
查看更多
登录 后发表回答