Wednesday, January 28, 2009

C# codebehind Populating Dropdownlist with years

I was working on a project at work which is a little lenghty in describing, but basically I needed a way to get out of constantly being interrupted by staff when one or a dozen pdf files need to be updated or added to the website.

At my job there are only two developers. Bob has over 30 years experience programming, but no C#. So I was tasked with helping him. It wasn't difficult because he knows Java, but getting him to understand how that interacts with asp.net web page controls took time.

Because we are few in number, anything that has to do with programming we do it. That also includes updates daily to the website. When I first arrive 70% of my interruptions were due to web updates, deletions and so forth. My boss wanted me to get out of the web page maintenance job, unfortunately there wasn't any money to hire anyone else.

So I came up with a solution, well, many solutions. Create pages that are dynamic and interactive.

Here is one solution I solved:

In the past all pdf documents were stored in one single directory. Naming conventions varied based on who created the pdf and copied it to the web server. Links on the site broke constantly, files were overwritten without regard to who may need them.

What I did was created a web page to manage the content. Created several tables in the database to manage the information. The pages contained controls such as Labels, Upload Control, TextBox Controls and Dropdownlist.

I provided the user of the control with a visual of what the name of the pdf should be based on their selections from the dropdownlist Controls, TextBox Controls and so forth. This help to keep a consistent naming standard.

I also had the page load method check for the current year, then pass that value into if(!Directory.Exists) statement to see if that year exists, if not, then I tell it to create a new folder and pass the path to the Upload Control and the insert statement and now we have our solution.

Here is a way to dynamically populate a dropdownlist control with years:

In your method or page load method:

int _currentyear;
const int _year = 2008

Keep in mind the Datetime.Now.Year returns an int.

protected void Page_Load(object sender, EventArgs e)
{
int _currentyear;
const int _year = 2008;
_currentyear = DateTime.Now.Year;
for (int s = _year; s <= _currentyear; s++)
{
DropDownList1.Items.Add(new ListItem((s).ToString(),(s).ToString()));
}
DropDownList1.DataBind();
}


Make sure you drop a dropdownlist control on your webpage or this will not work;

Now if you want to do the years in reverse order change your for statement:
for(int s = _currentyear; s >=_year; s--)

If year is 2009 and you want that year to be the selected text, you will need to change your code slightly to block the code in a if(!IsPostBack)

Then add: DropDownList1.SelectedValue = _currentyear;

2 comments:

retirethis said...

thanks!

retirethis said...

Thank you. you know it's sincere because I had to submit 10x before I got the captcha right.