Hi nauna,
Check this example. Now please take its reference and correct your code.
HTML
<asp:TextBox ID="TextBox1" runat="server" TextMode="Date"
OnTextChanged="DateChanged" AutoPostBack="true"></asp:TextBox>
<asp:DropDownList runat="server" ID="ddlTimes">
</asp:DropDownList>
Code
C#
protected void DateChanged(object sender, EventArgs e)
{
DateTime selectedDate = Convert.ToDateTime(TextBox1.Text.Trim());
List<ListItem> times = GetTime();
for (int i = times.Count; i > 0; --i)
{
DateTime dt;
if (!DateTime.TryParse(times[i - 1].Text, out dt)) { }
if (dt <= DateTime.Now && selectedDate == DateTime.Today)
{
times.RemoveAt(i - 1);
}
}
ddlTimes.DataSource = times;
ddlTimes.DataTextField = "Text";
ddlTimes.DataValueField = "Value";
ddlTimes.DataBind();
}
private List<ListItem> GetTime()
{
List<ListItem> times = new List<ListItem>();
TimeSpan timeSpan = new TimeSpan(12, 00, 00);
string time = DateTime.Today.Add(timeSpan).ToString("hh:mm tt");
times.Add(new ListItem { Text = time, Value = time });
timeSpan = new TimeSpan(14, 00, 00);
time = DateTime.Today.Add(timeSpan).ToString("hh:mm tt");
times.Add(new ListItem { Text = time, Value = time });
timeSpan = new TimeSpan(17, 00, 00);
time = DateTime.Today.Add(timeSpan).ToString("hh:mm tt");
times.Add(new ListItem { Text = time, Value = time });
timeSpan = new TimeSpan(19, 00, 00);
time = DateTime.Today.Add(timeSpan).ToString("hh:mm tt");
times.Add(new ListItem { Text = time, Value = time });
timeSpan = new TimeSpan(08, 00, 00);
time = DateTime.Today.Add(timeSpan).ToString("hh:mm tt");
times.Add(new ListItem { Text = time, Value = time });
timeSpan = new TimeSpan(10, 00, 00);
time = DateTime.Today.Add(timeSpan).ToString("hh:mm tt");
times.Add(new ListItem { Text = time, Value = time });
return times;
}
VB.Net
Protected Sub DateChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim selectedDate As DateTime = Convert.ToDateTime(TextBox1.Text.Trim())
Dim times As List(Of ListItem) = GetTime()
For i As Integer = times.Count To 1 Step -1
Dim dt As DateTime
If Not DateTime.TryParse(times(i - 1).Text, dt) Then
End If
If dt <= DateTime.Now AndAlso selectedDate = DateTime.Today Then
times.RemoveAt(i - 1)
End If
Next
ddlTimes.DataSource = times
ddlTimes.DataTextField = "Text"
ddlTimes.DataValueField = "Value"
ddlTimes.DataBind()
End Sub
Private Function GetTime() As List(Of ListItem)
Dim times As List(Of ListItem) = New List(Of ListItem)()
Dim timeSpan As TimeSpan = New TimeSpan(12, 0, 0)
Dim time As String = DateTime.Today.Add(timeSpan).ToString("hh:mm tt")
times.Add(New ListItem With {.Text = time, .Value = time})
timeSpan = New TimeSpan(14, 0, 0)
time = DateTime.Today.Add(timeSpan).ToString("hh:mm tt")
times.Add(New ListItem With {.Text = time, .Value = time})
timeSpan = New TimeSpan(17, 0, 0)
time = DateTime.Today.Add(timeSpan).ToString("hh:mm tt")
times.Add(New ListItem With {.Text = time, .Value = time})
timeSpan = New TimeSpan(19, 0, 0)
time = DateTime.Today.Add(timeSpan).ToString("hh:mm tt")
times.Add(New ListItem With {.Text = time, .Value = time})
timeSpan = New TimeSpan(8, 0, 0)
time = DateTime.Today.Add(timeSpan).ToString("hh:mm tt")
times.Add(New ListItem With {.Text = time, .Value = time})
timeSpan = New TimeSpan(10, 0, 0)
time = DateTime.Today.Add(timeSpan).ToString("hh:mm tt")
times.Add(New ListItem With {.Text = time, .Value = time})
Return times
End Function
Screenshot