C# drop down list Databinding

I have a C# DropDownList that I set the DataSource for in the following way in the OnPreRender() method.  What happens is that after the user changes the value in the drop down list, the page refreshes, and the data source re-fills the DropDownList correctly, but automatically goes back to the first value.  I was just wondering how to make this so that it stays with the value the user selected.

 protected override void OnPreRenderComplete(EventArgs e)
                SqlConnection sqlConnectionString88 = new SqlConnection("Data Source=server;Initial Catalog=database;Persist Security Info=True;User ID=userid;Password=password");
        SqlCommand cmd88 = new SqlCommand();
        SqlDataReader reader88;
        cmd88.Parameters.Add(new SqlParameter("@Proj", SqlDbType.Int, 10));
        cmd88.Parameters["@Proj"].Value = projectselected;
        cmd88.CommandText = "SELECT column1 FROM table1 WHERE column2 = @Proj AND column3 = 'False'";
        cmd88.CommandType = CommandType.Text;
        cmd88.Connection = sqlConnectionString88;
        reader88 = cmd88.ExecuteReader();
        DropDownList2.DataSource = reader88;
        DropDownList2.DataTextField = "VersionName";

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Daniel Van Der WerkenIndependent ConsultantCommented:
Question: Does the data in the dropdown need to change or is it populated once and then keeps all the values?

I would assume the dropdown list would be static.  So, the page would load and you'd grab the data from the database for the dropdownlist and that would be it, right?  If that is the case, then you should not be reading the data more than once per page load.  Therefore, put an if statement in this code and do:

if ( !Page.IsPostback )
   // fill up the dropdown list with info.

Now, if you are still losing the values, then use a ViewState.  Like ViewState[ "myCurrentDropdownListPosition" ] = 5.

Then, when you get a postback on the page, check the ViewState and set the dropdown list to that position.

int myCurrentDropdownListPosition = int.MinValue;

int.TryParse( ViewState[ "myCurrentDropdownListPosition" ].ToString(), out myCurrentDropdownListPosition );

DropDownList2.SelectedIndex = myCurrentDropdownListPosition;

something like that.  Watch for null values because it's possible the ViewState doesn't exist at some point when you're trying to check it.  I always create a ViewState["whatever"] in my OnInit() event and set it to null and then later check the value.

Make sense?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
No points just re-iterate what DanZel said. Usually you don't need to DataBind on every postback. If you only bind once the DropDownList items will automatically remain in ViewState (unless you have it turned off) and the SelectedItem will remain on subsequent postbacks as well.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.