dropdownlist fill at pageload and save to db
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 m开发者_如何转开发y db there is always the default value.
hmmm how solve this?
thanks
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.
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.
精彩评论