• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1331
  • Last Modified:

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?
0
jholmes724
Asked:
jholmes724
1 Solution
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now