Retaining Dropdownlist selectedvalue after postback in ASP.NET Web form.

Using VB.NET I have two dropdownlists which are populated by a typed dataset.  The Dropdown list values work fine, but at postback I want to be able to retain the selectedvalue.   I have done this within a datagrid template column using the databinder.eval(Container, DataItem.Fieldname) method.   But when trying this same method on dropdownlist on a form, there is no container item.  I have tired putting the follwoing statement  using my dataset in place of the container and data item.   Ex:  SelectedValue='<%# DataBinder.Eval(CfDataSet1, "Tables[CityCodeList].DefaultView.[0].CityCode") %>'>.   But obviously this did not work.    Can you tell me how this is accomplished?
jholmes724Asked:
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.

nisarkhanCommented:
have you tried put your code in

 If Not IsPostBack Then
  //ur code ?
 end if

0
jholmes724Author Commented:
Yes, the Not IsPostBack Code is there.   Dropdown list selectedvalue or selected index are not retain in viewstate unless you put in some kind of coding.   I did get it to work by putting 2 non visible text boxes on the web page and then on the ItemChanged Event I assigned the current SelectedIndex value (int) into the textbox.text property.  And then after postback and rebinding I set the dropdownlist.SelectedIndex value to the Ctype(textbox.tex, integer) and that work.  However it seems like a bit of a hack to me and wonder ig there is a more .Netish way of doing it like using the databinder.eval method, which I really am a little unclear of how it works.   Let me know if you come up with any thoughts on that.
0
deanvanrooyenCommented:
hi,

You must be binding the drop list again, what value does it revert back to? is it the value that was populated by the original databind?
Do you manualy bind it in the not post back check? or is it bound in the aspx page, i suggest manually bind in the not post back method this will resolve your issue,

eg
If Not IsPostBack Then
  create connection
  created dbadapter
  fill data set .....
  Bind droplist.....
 end if
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

jholmes724Author Commented:
hmm, well I am doing that, I manually bind in the not post back but that does not retain my selectedindex or selectedvalue.   The method I mentioned above is working but I just wondered if there is another way.  Thanks for the response.
0
Jason ScolaroCommented:
As long as you bind to the DropDownLists in the Not IsPostBack block, then ViewState will take over for each PostBack after that, you shouldn't be losing your SelectedIndex or SelectedValue unless you are re-binding to the DropDownList.  Perhaps you can show some of your code...

-- Jason
0

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
jholmes724Author Commented:
You were correct.  I had some downline code that was rebinding the dropdown list.  Once I got rid of that it worked fine.  Thanks.  That was a big help.
0
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
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.