How to set dynamically time interval in minutes in drop down list in asp.net
In this article, I will describe that how to bind
the time value in drop down list at runtime with time interval of specified
minutes. In this I have specified the 5 min interval, you can specify the time
gap according to your needs on project.
Design:-
Source
code:-
<%@
Page Language="C#" AutoEventWireup="true" CodeFile="ddlwithinterval.aspx.cs"
Inherits="ddlwithinterval" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style2
{
font-size:
xx-large;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<fieldset style="width: 500px">
<legend class="style2">Select Time </legend><span class="style2">From</span>:
<asp:DropDownList ID="ddlTimeFrom" runat="server" Width="150px">
</asp:DropDownList>
<span class="style2">To</span>:
<asp:DropDownList ID="ddlTimeTo" runat="server" Width="150px">
</asp:DropDownList>
</fieldset>
</div>
</form>
</body>
</html>
Code
bind (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 ddlwithinterval : System.Web.UI.Page
{
protected void Page_Load(object
sender, EventArgs e)
{
if
(!Page.IsPostBack)
{
BindTime();
}
}
private void BindTime()
{
DateTime
StartTime = DateTime.ParseExact("00:00", "HH:mm",
null);
DateTime
EndTime = DateTime.ParseExact("23:55", "HH:mm",
null);
//To set
hh/mm/ss interval
TimeSpan
Interval = new TimeSpan(0,
5, 0);
ddlTimeFrom.Items.Clear();
ddlTimeTo.Items.Clear();
while
(StartTime <= EndTime)
{
ddlTimeFrom.Items.Add(StartTime.ToShortTimeString());
ddlTimeTo.Items.Add(StartTime.ToShortTimeString());
StartTime =
StartTime.Add(Interval);
}
ddlTimeFrom.Items.Insert(0, new ListItem("--Select--", "0"));
ddlTimeTo.Items.Insert(0, new ListItem("--Select--", "0"));
}
}
Out Put:-
How to set dynamically time interval in minutes in drop down list in asp.net
Reviewed by NEERAJ SRIVASTAVA
on
12:10:00 PM
Rating:
No comments: