Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Sunday 8 March 2015

While working on asp.net project, we have the requirement to select time manually in hh: mm:SS (hours (24), minutes (59) and seconds (59)) in two digit format e.g. 00: 01: 02 etc in 3 different drop down list controls. So that user can select hour, minute and seconds manually.

In this article I will explain how to populate 3 different dropdownlist controls with time in the form of hours, minutes and seconds in asp.net using both C# and VB Languages.

Asp.Net C# Section

In the design section place 3 dropdownlist controls as:
<div>
        <fieldset style="width:400px;">
            <legend style="font-weight: 700">Populate Hour,Minute and Seconds in DropDownList</legend>
            <br />
            Hours:
            <asp:DropDownList ID="ddlHours" runat="server">
            </asp:DropDownList>
            Minutes:
            <asp:DropDownList ID="ddlMinutes" runat="server">
            </asp:DropDownList>
            Seconds:
            <asp:DropDownList ID="ddlSeconds" runat="server">
            </asp:DropDownList>
            <br />
        </fieldset>
    </div>

Asp.Net C# code to fill hours, minutes & seconds in DropDownList

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            PopulateHours();
            PopulateMinutes();
            PopulateSeconds();
        }
    }

    private void PopulateHours()
    {
        for (int i = 0; i <= 24; i++)
        {
            ddlHours.Items.Add(i.ToString("D2"));
        }       
    }
    private void PopulateMinutes()
    {
        for (int i = 0; i < 60; i++)
        {
            ddlMinutes.Items.Add(i.ToString("D2"));
        }
    }
    private void PopulateSeconds()
    {
        for (int i = 0; i < 60; i++)
        {
            ddlSeconds.Items.Add(i.ToString("D2"));
        }
    }

Asp.Net VB code to bind hours, minutes & seconds in DropDownList

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            PopulateHours()
            PopulateMinutes()
            PopulateSeconds()
        End If
    End Sub
    Private Sub PopulateHours()
        For i As Integer = 0 To 24
            ddlHours.Items.Add(i.ToString("D2"))
        Next
    End Sub
    Private Sub PopulateMinutes()
        For i As Integer = 0 To 59
            ddlMinutes.Items.Add(i.ToString("D2"))
        Next
    End Sub
    Private Sub PopulateSeconds()
        For i As Integer = 0 To 59
            ddlSeconds.Items.Add(i.ToString("D2"))
        Next


    End Sub

0 comments:

Post a Comment