Populate Hours,Minutes and Seconds in DropDownList in a single loop in Asp.Net C# ,VB

Populate Hours,Minutes and Seconds in DropDownList in a single loop in Asp.Net C# ,VB Introduction: In this article I am going to explain how to dynamically bind time i.e. Hours, Minutes and Seconds in DropDownList control in a single for loop in Asp.Net using both C# and VB Language.

Description: While working on Asp.Net project I got the requirement to fill hours, minutes and seconds in dropdownlist. There are numerous ways to do so but adopted an optimized way. As we know we need to fill from 0 up to 23 in hours and up to 59 in both minute and seconds dropdownlist. So we can loop through 0 to 59 and fill up to 23 in hours dropdownlist based on condition and upto full 59 in minutes and seconds dropdownlist as mentioned in the code below.

Implementation: Let’s create a sample web page to demonstrate the concept

HTML source 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <form id="form1" runat="server">
            <fieldset style="width: 240px; height: 360px;">
                <legend>Bind Time in DropDownList</legend>
                <asp:DropDownList ID="ddlHours" runat="server" />
                <asp:DropDownList ID="ddlMinutes" runat="server" />
                <asp:DropDownList ID="ddlSeconds" runat="server" />

protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
    private void BindTime()
        List<string> hours = new List<string>();
        List<string> minutes = new List<string>();
        List<string> seconds = new List<string>();

        for (int i = 0; i <= 59; i++)
            if (i < 24)
        ddlHours.DataSource = hours;
        ddlMinutes.DataSource = minutes;
        ddlSeconds.DataSource = seconds;

  Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
        End If
    End Sub
    Private Sub BindTime()
        Dim hours As New List(Of String)()
        Dim minutes As New List(Of String)()
        Dim seconds As New List(Of String)()

        For i As Integer = 0 To 59
            If i < 24 Then
            End If
        ddlHours.DataSource = hours
        ddlMinutes.DataSource = minutes
        ddlSeconds.DataSource = seconds

    End Sub

Now over to you:
A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates.
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..