How to Dynamically bind Hours,Minutes,Second in Asp.Net



Here we will see how to bind Hours, Minutes and Second in dropdown in asp.net. It’s pretty easy to bind it in asp.net dropdown controls. Here is Code snippets.

HTML CODE:
<div>
        <fieldset style="width: 434px;">
            <legend>Bind Time in DropDownList</legend>
HH: <asp:DropDownList ID="ddlHours" runat="server" Width="105px" />
       MM: <asp:DropDownList ID="ddlMinutes" runat="server" Width="94px" />
       SS: <asp:DropDownList ID="ddlSeconds" runat="server" Width="92px" />
        </fieldset>
    </div>

CODE BEHIND:
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTime();
            }
        }
        private void BindTime()
        {
            List<string> hours = new List<string>();
            List<string> minutes = new List<string>();
            List<string> seconds = new List<string>();
            for (int i = 0; i <= 59; i++)
            {
                if (i < 24)
                {
                    hours.Add(i.ToString("00"));
                }
                minutes.Add(i.ToString("00"));
                seconds.Add(i.ToString("00"));
            }
            ddlHours.DataSource = hours;
            ddlHours.DataBind();
            ddlMinutes.DataSource = minutes;
            ddlMinutes.DataBind();
            ddlSeconds.DataSource = seconds;
            ddlSeconds.DataBind();
        }

Run your Webpage in Browser and see.

Related Posts

Previous
Next Post »

Thanks for comments.....