Monday, 21 October 2013

Bind DropDownList Dynamically With Month Name in ASP.NET C#/VB.NET Web Application

Hi friends,in this article I will explain about  Bind DropDownList Dynamically With Month Name in ASP.NET C#/VB.NET Web Application.
I already explained in the previous articles about How to create Fixed Menu When Scrolling Page with CSS and jQuery or JQuery - Leave menu bar fixed on top when scrolled or Creating a Floating Navigation MenuHTML - How can I open multiple links/windows with a single anchor tag using Javascript and MVC4 Razor:How to call Stored Procedure using C#

Root project -- Add New Item -- select Web Form and name it as BindMonths.aspx and write the following code.
In ASP.NET:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Bind DropDownList Dynamically With Month Name in ASP.NET C#/VB.NET Web Application.</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="txtMonth" runat ="server"></asp:TextBox>
    <asp:DropDownList runat="server" ID="ddlMonths">     </asp:DropDownList>
    <asp:Button ID="btnSubmit" runat ="server" Text ="Submit"
            onclick="btnSubmit_Click" />
    </div>
    </form>
</body>
</html>

In C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class BindMonths : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetMyMonthList();
        }
    }
    public void GetMyMonthList()
    {
        DateTime month = Convert.ToDateTime("1/1/2012");
        for (int i = 0; i < 12; i++)
        {
            DateTime nextMonth = month.AddMonths(i);
            ListItem list = new ListItem();
            list.Text = nextMonth.ToString("MMMM");
            list.Value = nextMonth.Month.ToString();
            ddlMonths.Items.Add(list);
        }
        ddlMonths.Items.Insert(0, new ListItem("  Select Month  ", "0"));
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        txtMonth.Text = ddlMonths.SelectedItem.ToString();
    }
}


In VB.NET:
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Partial Public Class BindMonths
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(sender As Object, e As EventArgs)
        If Not IsPostBack Then
            GetMyMonthList()
        End If
    End Sub
    Public Sub GetMyMonthList()
        Dim month As DateTime = Convert.ToDateTime("1/1/2012")
        For i As Integer = 0 To 11
            Dim nextMonth As DateTime = month.AddMonths(i)
            Dim list As New ListItem()
            list.Text = nextMonth.ToString("MMMM")
            list.Value = nextMonth.Month.ToString()
            ddlMonths.Items.Add(list)
        Next
        ddlMonths.Items.Insert(0, New ListItem("  Select Month  ", "0"))
    End Sub
    Protected Sub btnSubmit_Click(sender As Object, e As EventArgs)
        txtMonth.Text = ddlMonths.SelectedItem.ToString()
    End Sub
End Class

Save and run the page the output of the above page as shown in the figure.
"If you like my blog or articles, you can appreciate by leaving your comments or Liking my Facebook pageAspdotnet-kishore, following on Google+ Aspdotnet-Kishore, Twitter  on AspdotnetKishore, Linked in Aspdotnet-Kishore, stumbling my posts on stumble upon and subscribing on  RSSfeed Aspdotnet-Kishore for free updates directly to your Email inbox . Watch my blog  for more articles."

No comments:

Post a Comment

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.