Here is a sample that does what you need, and is highly flexible.
First the page itself (test page) - there are two drop downs here, as the
code is set up to setup as many time dropdowns as you need.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp
ropDownList ID="DropDownList1" runat="server">
</asp
ropDownList>
<asp
ropDownList ID="DropDownList2" runat="server">
</asp
ropDownList>
</div>
</form>
</body>
</html>
Now the .cs file
using System;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DropDownList ddl = this.DropDownList1;
LoadTimeDropDownList(ref ddl, 8, 17, 30);
ddl = this.DropDownList2;
LoadTimeDropDownList(ref ddl, 6, 22, 60);
}
private void LoadTimeDropDownList(ref DropDownList ddl, int startHour,
int endHour, int incrementInMinutes)
{
DateTime now = DateTime.Now;
DateTime startTime = new DateTime(now.Year, now.Month, now.Day,
startHour, 0, 0);
DateTime endTime = new DateTime(now.Year, now.Month, now.Day,
endHour, 0, 0);
LoadTimeDropDownList(ref ddl, startTime, endTime,
incrementInMinutes);
}
private void LoadTimeDropDownList(ref DropDownList ddl, DateTime
startTime, DateTime endTime, int incrementInMinutes)
{
DateTime now = DateTime.Now;
bool haveIndex = false;
while (startTime <= endTime)
{
ddl.Items.Add(startTime.ToShortTimeString());
startTime = startTime.AddMinutes(incrementInMinutes);
if ((startTime > now) && (!haveIndex))
{
ddl.SelectedIndex = ddl.Items.Count - 1;
haveIndex = true;
}
}
}
}
This is a down and dirty try at this, but you should see two different
drop downs, with different parameters. You can encapsulate this in a user
control and use it over and over again, by creating parameters for start
hour, end hour and increment.
--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA
Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss
or just read it:
http://gregorybeamer.spaces.live.com/
********************************************
| Think outside the box! |
********************************************