how to add data in asp.net dropdownlist

RAVI 1,076 Reputation points
2024-03-08T06:16:32.2466667+00:00

Hello

Example 1 :- Assume Current Month Is MAR-2024 and my asp.net dropdownlist like this

<asp:DropDownList ID=“Sample” runat=“server”>  
</asp:DropDownList>

I need my drodown list to be shown like this of past 12 months

MAR-2024

FEB-2024
JAN-2024
DEC-2023
NOV-2023
OCT-2023
SEP-2023
AUG-2023
JUL-2023
JUN-2023
MAY-2023
APR-2023

Example 2 :- Assume Current Month Is APR-2024 and my asp.net dropdownlist like this
APR-2024
MAR-2024
FEB-2024

JAN-2024

DEC-2023

NOV-2023

OCT-2023

SEP-2023

AUG-2023

JUL-2023

JUN-2023

MAY-2023

how to add this in my dopdownlist based on current month and it bind data like this for past 12 months using c# code or javascript

Thanking You

my drodpwnlist to be look like this

Developer technologies ASP.NET Other
{count} votes

Accepted answer
  1. Albert Kallal 5,586 Reputation points
    2024-03-09T17:39:43.1333333+00:00

    Assuming this markup:

            <asp:DropDownList ID="cboMonth" runat="server"
    
                DataTextField="text"
    
                DataValueField="value" >
    
            </asp:DropDownList>
    

    Then this code should work quite well:

            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    LoadCombo();
                }
            }
    
            void LoadCombo()
            {
                DateTime Today = DateTime.Now;
                DateTime dtStart = new DateTime(Today.Year, Today.Month, 1);
    
                for (int i = 1;i <= 12;i++)
                {
                    ListItem OneMonth = new ListItem();
                    OneMonth.Text = dtStart.ToString("MMM - yyyy");
                    cboMonth.Items.Add(OneMonth);
                    dtStart = dtStart.AddMonths(-1);
                }
            }
    
    

    And the result is this:

    cbomonths


2 additional answers

Sort by: Most helpful
  1. SurferOnWww 4,631 Reputation points
    2024-03-09T01:46:03.86+00:00

    Please try the following:

    using System;
    using System.Collections.Generic;
    
    namespace WebForms1
    {
        public class Item
        {
            public string TextField { get; set; }
            public string ValueField { get; set; }
        }
    
        public partial class WebForm36 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                Sample.DataSource = CreateDataSource(DateTime.Now);
                Sample.DataTextField = "TextField";
                Sample.DataValueField = "ValueField";
                Sample.DataBind();
            }
    
            protected List<Item> CreateDataSource(DateTime date)
            {
                var list = new List<Item>();
                for (int i = 0; i < 12; i++)
                {
                    var item = new Item
                    {
                        TextField = GetYearMonth(date.AddMonths(-i)),
                        ValueField = GetYearMonth(date.AddMonths(-i))
                    };
    
                    list.Add(item);
                }
    
                return list;
            }
    
            protected string GetYearMonth(DateTime date)
            {
                int year = date.Year;
                int month = date.Month;
                switch (month)
                {
                    case 1:
                        return $"JAN-{year}";
                    case 2:
                        return $"FEB-{year}";
                    case 3:
                        return $"MAR-{year}";
                    case 4:
                        return $"APR-{year}";
                    case 5:
                        return $"MAY-{year}";
                    case 6:
                        return $"JUN-{year}";
                    case 7:
                        return $"JUL-{year}";
                    case 8:
                        return $"AUG-{year}";
                    case 9:
                        return $"SEP-{year}";
                    case 10:
                        return $"OCT-{year}";
                    case 11:
                        return $"NOV-{year}";
                    case 12:
                        return $"DEC-{year}";
                    default:
                        return null;
                }
            }
        }
    }
    
    <%@ Page Language="C#" AutoEventWireup="true" 
        CodeBehind="WebForm36.aspx.cs" Inherits="WebForms1.WebForm36" %>
    
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:DropDownList ID="Sample" runat="server">  
            </asp:DropDownList>
        </form>
    </body>
    </html>
    

    result

    result

    Please note that the above shows server local time.


  2. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2024-03-11T03:44:42.4966667+00:00

    Hi @RAVI,

    You can try using Enumerable.Range method to achieve this.

     <asp:DropDownList runat="server" ID="ddl">
     </asp:DropDownList>
    
     protected void Page_Load(object sender, EventArgs e)
     {
         if (!this.IsPostBack)
         {             
             ddl.DataSource = Enumerable.Range(0, 12).Select(i => DateTimeOffset.Now.AddMonths(-i).ToString("MMM-yyyy").ToUpper()); ;
             ddl.DataBind();
         }
     }
    

    User's image

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.