Here i am showing time in Label as 09AM. So if am having this format in label then i must have to Bind the DropDownList with these kind of values
01AM
02AM
- - -
12AM
HTML:
<form id="form1" runat="server">
<div>
<asp:Label ID="lblTime" runat="server" />
<br />
<asp:DropDownList ID="ddlTime" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlTime_OnSelectedIndexChanged">
<asp:ListItem Text="01AM" />
<asp:ListItem Text="02AM" />
<asp:ListItem Text="03AM" />
<asp:ListItem Text="04AM" />
<asp:ListItem Text="05AM" />
<asp:ListItem Text="06AM" />
<asp:ListItem Text="07AM" />
<asp:ListItem Text="08AM" />
<asp:ListItem Text="09AM" />
<asp:ListItem Text="10AM" />
<asp:ListItem Text="11AM" />
<asp:ListItem Text="12AM" />
<asp:ListItem Text="01PM" />
<asp:ListItem Text="02PM" />
<asp:ListItem Text="03PM" />
<asp:ListItem Text="04PM" />
<asp:ListItem Text="05PM" />
<asp:ListItem Text="06PM" />
<asp:ListItem Text="07PM" />
<asp:ListItem Text="08PM" />
<asp:ListItem Text="09PM" />
<asp:ListItem Text="10PM" />
<asp:ListItem Text="11PM" />
<asp:ListItem Text="12PM" />
</asp:DropDownList>
</div>
</form>
C#:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.lblTime.Text = DateTime.Now.ToString("hhtt");
}
}
protected void ddlTime_OnSelectedIndexChanged(object sender, EventArgs e)
{
if (this.lblTime.Text != this.ddlTime.SelectedItem.Text)
{
string message = "Time should match with above time";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script type = 'text/javascript'>");
sb.Append("window.onload=function(){");
sb.Append("alert('");
sb.Append(message);
sb.Append("')};");
sb.Append("</script>");
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());
}
}
Thank You.