Newbie: Selecting a value in a drop down

Please indulge a newbie question…

I have three dropdowns that are used to choose a date (day, month and year). I want them to initially display today’s date. They are being populated with code like this:

   // Populate the myRDate DropDown

       for (int i = 0; i < 31; i++)
            myRDate.Items.Add((i + 1).ToString());
            myRDate.Items[i].Value = (i + 1).ToString();
            [B]myRDate.SelectedIndex = DateTime.Now.Day[/B];

So the user changes the value to whatever they want and hits the Submit button and I try to collect the values they have chosen…

DateTime returnDate = new DateTime(Convert.ToInt32(myRYear.SelectedValue), Convert.ToInt32(myRMonth.SelectedValue), Convert.ToInt32(myRDate.SelectedValue));

But the #@#$!@# thing returns today’s date!

This is not what I want obviously. Can anyone tell me where I’m going wrong? Why doesn’t it return the date as chosen?

If I remove the myRDate.SelectedIndex = DateTime.Now.Day line from the initial population statement the 3 dropdowns are set to 1/1/2010 but the chosen values are passed to the database correctly.

Is there another way to set the initial selection that will not affect the dropdown for all time…

Any help very much appreciated.


hmmm. This is a strange way of binding to the day list, but ok. The only reason something like that might not work is if you are running that method on page load and not insead a if (!Page.IsPostBack) check

Thanks Nightstalker,

If it’s a strange way to bind, then I’m really, really open to suggestions of better ways to do things. I’m learning this stuff out of a book, adapting as I go.

The code to populate the dropdown runs at page load but the other runs inside the …

protected void submitButton_Click(object sender, EventArgs e)
if (Page.IsValid)


There is nothing wrong with that way of binding. Just a bit confusing.

Just make sure in the page load if your you check !Page.IsPostBack when binding.

for (int i = 1; i <= 31; i++)

The penny drops! Thanks Nightstalker.