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

Posted on 2006-03-25
Last Modified: 2012-06-27
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?
Question by:jholmes724
    LVL 7

    Expert Comment

    have you tried put your code in

     If Not IsPostBack Then
      //ur code ?
     end if


    Author Comment

    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.
    LVL 12

    Expert Comment


    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,

    If Not IsPostBack Then
      create connection
      created dbadapter
      fill data set .....
      Bind droplist.....
     end if

    Author Comment

    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.
    LVL 22

    Accepted Solution

    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

    Author Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now