Hi itsme,
Check this example. Now please take its reference and correct your code.
HTML
<fieldset style="width: 250px">
<legend>Select Time </legend>
From: <asp:DropDownList ID="ddlTimeFrom" runat="server" OnSelectedIndexChanged="SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
To: <asp:DropDownList ID="ddlTimeTo" runat="server" OnSelectedIndexChanged="SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
</fieldset><br />
<asp:Label ID="lblOpenTime" runat="server" />
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindTime();
}
}
protected void SelectedIndexChanged(object sender, EventArgs e)
{
List<String> mode = new List<String>();
if (ddlTimeFrom.SelectedIndex > 0 && ddlTimeTo.SelectedIndex > 0)
{
mode.Add(ddlTimeFrom.Text);
mode.Add(ddlTimeTo.Text);
lblOpenTime.Text = "Open Time : " + string.Join(", ", mode);
}
else
{
lblOpenTime.Text = "";
}
}
private void BindTime()
{
DateTime StartTime = DateTime.ParseExact("00:00", "HH:mm", null);
DateTime EndTime = DateTime.ParseExact("23:55", "HH:mm", null);
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"));
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
BindTime()
End If
End Sub
Protected Sub SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim mode As List(Of String) = New List(Of String)()
If ddlTimeFrom.SelectedIndex > 0 AndAlso ddlTimeTo.SelectedIndex > 0 Then
mode.Add(ddlTimeFrom.Text)
mode.Add(ddlTimeTo.Text)
lblOpenTime.Text = "Open Time : " & String.Join(", ", mode)
Else
lblOpenTime.Text = ""
End If
End Sub
Private Sub BindTime()
Dim StartTime As DateTime = DateTime.ParseExact("00:00", "HH:mm", Nothing)
Dim EndTime As DateTime = DateTime.ParseExact("23:55", "HH:mm", Nothing)
Dim Interval As TimeSpan = 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)
End While
ddlTimeFrom.Items.Insert(0, New ListItem("--Select--", "0"))
ddlTimeTo.Items.Insert(0, New ListItem("--Select--", "0"))
End Sub
Screenshot