Introduction:
In this article i am going to explain how to fill/bind/populate 3 different dropdownlist
controls with time in the form of hours, minutes and seconds in asp.net using both C# and VB
Languages.
Description: While working on
asp.net project i got the requirement to fill hours (23), 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 to mark his check in
or check out time. This example will help other developers to accomplish this
kind of requirement.
Implementation: Let's create a simple webpage to demonstrate
the concept.
Asp.Net C# Section
- In the <Form> tag of the design page (e.g. default.aspx) place 3 dropdownlist controls as:
<div>
<fieldset style="width:330px;">
<legend>Populate Hour,Minute and Seconds in DropDownList</legend>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>
</fieldset>
</div>
Asp.Net C# code to fill hours, minutes
and 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
<= 23; i++)
{
ddlHours.Items.Add(i.ToString("D2"));
}
}
private void
PopulateMinutes()
{
for (int i = 0; i
<=59; i++)
{
ddlMinutes.Items.Add(i.ToString("D2"));
}
}
private void
PopulateSeconds()
{
for (int i = 0; i
<=59; i++)
{
ddlSeconds.Items.Add(i.ToString("D2"));
}
}
Asp.Net VB code to bind hours, minutes
and 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 23
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
" I hope you have learned how to bind dropdownlist with hours, minutes and seconds in asp.net with this example and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in 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."
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..