What is the best way to code up a Month and Year drop down list for ASP.NET?

后端 未结 8 1494
北荒
北荒 2020-12-16 00:31

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

相关标签:
8条回答
  • 2020-12-16 00:58

    Here is my solution, which is very similar to @jesse-brown's solution (the accepted answer)

    VB.NET:

    In a global functions class:

    Public Shared Function GetMonthList() As Generic.Dictionary(Of String, String)
        Dim months As New Generic.Dictionary(Of String, String)()
        For m As Int32 = 1 To 12
            months.Add(String.Format("{0:0#}", m), CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(m))
        Next
    
        Return months
    End Function
    

    On the ASPX page:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ddMonth.DataSource = GlobalFunctions.GetMonthList()
        ddMonth.DataValueField = "Key"
        ddMonth.DataTextField = "Value"
        ddMonth.DataBind()
    
    End Sub
    

    This implementation is in VB.NET because that happens to be what this webapp is using (legacy), however thank you very much for the examples in C# (my preferred language), I'm posting the VB.NET here to help the VB.NET community as well.

    0 讨论(0)
  • 2020-12-16 01:01

    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()));
            }
        }
    
    0 讨论(0)
  • 2020-12-16 01:07

    /Try this code to bind all month in dropdown list without using database/

     public void GetMonthList(DropDownList ddl)
        {
            DateTime month = Convert.ToDateTime("1/1/2000");
            for (int i = 0; i < 12; i++)
            {
                DateTime NextMont = month.AddMonths(i);
                ListItem list = new ListItem();
                list.Text = NextMont.ToString("MMMM");
                list.Value = NextMont.Month.ToString();
                ddl.Items.Add(list);
            }
            //ddl.Items.Insert(0, "Select Month");
            ddl.Items.FindByValue(DateTime.Now.Month.ToString()).Selected = true;
        }
    
    0 讨论(0)
  • 2020-12-16 01:19

    Extending @Jesse Brown's answer...

    With a using System.Globalization directive, I have the following code:

    for (int x = 0; x < 12; x++)
    {
        cboMonth.Items.Add
        (
           (x+1).ToString("00") 
           + " " 
           + CultureInfo.CurrentCulture.DateTimeFormat.MonthNames.GetValue(x)
         );
    }
    

    This produces a dropdown list that looks like:

    01 January 02 February 03 March ... 12 December

    A further refinement might be to make the displayed month the current month by adding:

    cboMonth.Text = DateTime.Now.Month.ToString("00") 
       + " " 
       + CultureInfo.CurrentCulture.DateTimeFormat.MonthNames.GetValue(DateTime.Now.Month);
    

    After the for loop.

    0 讨论(0)
  • 2020-12-16 01:20

    You could use this to get a list of all the Month names and loop through it.

    CultureInfo.CurrentCulture.DateTimeFormat.MonthNames
    

    You can use it like this...using the index of the Month as the value for your dropdown

    var months = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames;
    for (int i = 0; i < months.Length; i++)
    {
         ddl.Items.Add(new ListItem(months[i], i.ToString()));
    }
    
    0 讨论(0)
  • 2020-12-16 01:20
     public void bind_month()
        {
            var months = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames;
    
            ddl_month.Items.Insert(0, new ListItem("--Select--", "0"));
    
            for (int i = 0; i < months.Length-1; i++)
            {
                ddl_month.Items.Add(new ListItem(months[i],(i+1).ToString()));
            }
    
        }
    
    0 讨论(0)
提交回复
热议问题