Tuesday, March 13, 2012

Troubles with DropDownList

Hi

I've got problem with DropDownList, because when I change values, I've still got the first value of List.

So, how to set new selected value of DropDownList?I guess you have the following code concept in your page: "on a page_load, retrieve the data from a database or some other source and bind it to the control". This is right but incomplete. In fact Page_Load is fired also on a postback, so when a user selects something in the dropdownlist and the page is posted back (automatically or manually) to the server, the Page_Load is executed again. In order to execute the binding code only when needed, use this skeleton of code:

if (!Page.IsPostBack) {
//do the binding
}
//do other things that are not affected by a postback
.

Now, you're rebinding the controls every time the page is loaded; this code will perform the binding only when no postback is done (thus, only on the first request to the page).
Great Thanks
Now everything is working :)

0 comments:

Post a Comment