AllInWorld99 provides a reference manual covering many aspects of web programming, including technologies such as HTML, XHTML, CSS, XML, JavaScript, PHP, ASP, SQL,FLASH, jQuery, java, for loop, switch case, if, if else, for...of, for...in, for...each,while loop, blogger tips, blogger meta tag generator, blogger tricks, blogger pagination, client side script, html code editor, javascript editor with instant output, css editor, online html editor, materialize css tutorial, materialize css dropdown list,break, continue statement, label,array, json, get day and month dropdown list using c# code, CSS button,protect cd or pendrive from virus, cordova, android example, html and css to make android app, html code play,telerik show hide column, Transparent image convertor, copy to clipboard using javascript without using any swf file, simple animation using css, SQL etc. AllInWorld99 presents thousands of code examples (accompanied with source code) which can be copied/downloaded independantly. By using the online editor provided,readers can edit the examples and execute the code experimentally.


Bind Date and Year Drop down list in asp.net

     The following code is show how to bind the drop down list box from server-side function in asp.net.

Create dropdown List box in asp.net

     The following code is used to create two label and two drop down list box.

<asp:Label ID="Label2" runat="server" Text="Select Year"></asp:Label>
<asp:DropDownList ID="ddlyear" runat="server">
</asp:DropDownList>

<asp:Label ID="Label3" runat="server" Text="Select Month"></asp:Label>
<asp:DropDownList ID="ddlmonth" runat="server">
</asp:DropDownList>

You should include the following namespace.

using System.Globalization;
Defines how System.DateTime values are formatted and displayed, depending on the culture.

Bind Year and Month in Dropdown list box

      Two function are defined in the page load and every function bind the month and year in the drop down list box.

protected void Page_Load(object sender, EventArgs e)
{
 bind_month_ddl();
 bind_year_ddl();
}
private void bind_year_ddl()
{
 int year = (System.DateTime.Now.Year);
for (int intCount = year; intCount >= 1980; intCount--)
{ ddlyear.Items.Add(intCount.ToString());
} } private void bind_month_ddl() { for (int i = 1; i <= 12; i++) { ddlmonth.Items.Add(new System.Web.UI.WebControls.ListItem(DateTimeFormatInfo.CurrentInfo.GetMonthName(i), i.ToString())); } }

Output :
Bind months and years drop down list in asp.net


























2 comments:

Total Pageviews