|
-
Feb 2nd, 2004, 06:05 AM
#1
Thread Starter
Member
populate date in drop down list
Hi Guys,
I am trying to get populated the date in the dropdown list, and I wrote the code in c#
ie
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
//DateTime currentDate = new DateTime();
//public string currentYear = currentDate.Year.ToString;
this.PopulateYear();
Response.Write(DateTime.Now.Year);
}
public void PopulateYear()
{
for(int i = 2000; i <= (DateTime.Now.Year)+ 5; i++)
{
if (i == DateTime.Now.Year)
{
ddlYear.SelectedItem.Selected = true;
}
ListItem item = new ListItem(i.ToString(),i.ToString());
ddlYear.Items.Add(item);
}
}
Here I am trying to display the current year as slected year. but I dunno for some reason it selected 2000 instead of 2004
please anyone help me to solve this...
Cheers,
dummy
-
Feb 2nd, 2004, 12:59 PM
#2
Re: populate date in drop down list
Code:
private void Page_Load(object sender, System.EventArgs e)
{
this.PopulateYear();
Response.Write(DateTime.Now.Year);
}
public void PopulateYear()
{
int max = DateTime.Now.Year + 5;
for(int i = 2000; i <= max; i++)
{
ListItem item = new ListItem(i.ToString(),i.ToString());
item.Selected = (i == DateTime.Now.Year);
ddlYear.Items.Add(item);
}
}
-
Feb 3rd, 2004, 06:28 AM
#3
Thread Starter
Member
Hi axion_sa, you are a star!
Thanks,
dummy
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|