views:

151

answers:

2

hi

in page_load I fill a drop down with values from db.

by clicking a button I like to insert the selected value of the dropdown into my db.

but because of the page load the selected value is resetted and in my db there is always the default value.

hmmm how solve this?

thanks

+2  A: 

try like...

if (!IsPostBack)
    {
        //fill your dropdownlist here

    }

As your page is postback, when you hit button and your dropdown again populated and you need to restrict this by putting postback check.

Muhammad Akhtar
thank you just at the moment I found another solution.I put the fill method into page_prerender.thanky for your interest to help.
snarebold
If you fill in prerender you still override the selectedvalue, and the page ends up looking different from what you've just saved to db.This will definately get you some "funny" debugging issues later on. I recommend changing it into one of the solutions listed here instead.
Steffen
thanks actually I try it so.
snarebold
A: 

Do you use viewstate ?

If you use viewstate, don't fill on postbacks like Muhammad mentions.

If you don't use viewstate (some people prefer this, myself included) fill the dropdownlist in your OnInit event and grab SelectedValue in your button's Click handler.

It's important that you fill the dropdownlist in OnInit in this case, since the selectedvalue is set in between Init and Load. So if you fill it OnLoad, you override the selectedvalue it just had gotten.

It pretty much goes like this:

OnInit
Set values from Request.Form
OnLoad

Obviously a lot more happens, but these are the important steps in this scenario.

Steffen