How to dynamically bind the drop down list with months Name without using Database in asp.net C#

In this article, I will bind the dropdownlist with months name without using data base for this I have used a globalization. Generally we need to this code in registration from  for Date of birth , booking month and so.on


 
Source Code:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="dynamiciallybindmonth.aspx.cs" Inherits="dynamiciallybindmonth" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">


    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:DropDownList ID="DropDownList1" runat="server" >
        </asp:DropDownList>
   
    </div>
    </form>
</body>
</html>


Code behind(c#):-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;

public partial class dynamiciallybindmonth : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        bindddl_month();
    }




    private void bindddl_month()
    {
        DateTimeFormatInfo info = DateTimeFormatInfo.GetInstance(null);
        for (int i = 1; i < 13; i++)
        {
            DropDownList1.Items.Add(new ListItem(info.GetMonthName(i), i.ToString()));
        }
    }
}



Out-Put:-


How to dynamically bind the drop down list with months Name without using Database in asp.net C# How to dynamically bind the drop down list with months Name without using Database in asp.net C# Reviewed by NEERAJ SRIVASTAVA on 2:24:00 PM Rating: 5

No comments:

Powered by Blogger.