Dropdown List doesn't know what's in it's list

Discussion in 'ASP .Net Web Controls' started by Bryan Stauffer, Jul 6, 2006.

  1. I've got a web form with two dropdown list controls. The first one is
    configured to "AutoPostBack" so that the second one can be loaded according
    to whatever has been selected in the first. This would be nice to with an
    asynchronous callback but I'm not there yet so this works OK. So after the
    user has entered some data and clicks a "next" button I save all the data to
    my Order object and save that to the Session for laster use. All of this
    works. The problem is that when I come back to this page and try to get the
    controls to display the data that the user selected I get the "has a
    SelectedValue which is invalid because it does not exist in the list of
    items" error. I've made sure the dropdown list is actually loaded with the
    valid values and in fact it is! When I'm debugging everything goes smoothly
    right until the browser attempts to render the page. Any ideas or
    suggestions are greatly appreciated!

    protected void Page_Load(object sender, EventArgs e)
    cart = ((clsCart)Session["cart"]);
    string propertyType = "Commercial";


    switch (cart.Order.PropertyType)
    case PropertyType.Commercial:
    this.drpPropertyType.SelectedValue = "Commerical";
    case PropertyType.Residential:
    this.drpPropertyType.SelectedValue = "Residential";

    this.drpLoanType.SelectedValue = cart.Order.LoanType;

    protected void SetLoanTypes(string PropertyType)
    XmlDocument xml = new XmlDocument();
    switch (PropertyType)
    case "Commercial":
    case "Residential":
    throw new Exception("Invalid PropertyType in SetLoanTypes()");
    XmlNodeList nlLoanTypes = xml.SelectNodes("loan_types/loan_type");
    foreach (XmlNode n in nlLoanTypes)
    Bryan Stauffer, Jul 6, 2006
    1. Advertisements

  2. Bryan Stauffer

    CaffieneRush Guest

    Firstly, check to see if your first and second dropdownlist has the
    listitems with the values you selected in page_load.

    Also, when using SelectedValue, msdn recommends wrapping it in a
    try-catch block.
    ' Perform this operation in a try-catch block in case the item
    is not found.
    List.SelectedValue = ItemTextBox.Text
    MessageLabel.Text = "You selected " & List.SelectedValue +
    Catch ex As Exception
    List.SelectedValue = Nothing
    MessageLabel.Text = "Item not found in ListBox control."
    End Try

    Personally, I avoid the SelectedValue property when selecting an item
    in the dropdown. Instead, I find the item directly before selecting it.
    ' Selects the item whose text is Apples
    If Not li Is Nothing Then
    li.Selected = True
    End If


    CaffieneRush, Jul 6, 2006
    1. Advertisements

  3. Thank you for the ideas! I will try them immediately and let you know how
    it worked. Thanks again.

    Bryan Stauffer, Jul 7, 2006
  4. In response to your first point, I am loading the second list with the
    necessary values in the Page_Load and I can see that they are there in the
    debugger but once I step out of the Page_Load the list seems to "forget"
    what was in it because I get the error. Is it wrong to be loading the list
    before setting it's value?

    Bryan Stauffer, Jul 7, 2006
  5. I tried the suggestion but unfortunately it did not work. I used the
    following code block and there was no error:

    ListItem li = this.drpLoanType.Items.FindByText(cart.Order.LoanType);
    if (li != null)
    li.Selected = true;
    catch (Exception ex)

    This did not catch any error and once again I could see that the values did
    exist in the items collection of the dropdown because they were just loaded
    a few lines of code earlier.

    This seems to me like I'm doing something wrong by loading the dropdown list
    in the Page_Load. My control is called 'drpPropertyTypes' and here is the
    exact error message:

    'drpPropertyType' has a SelectedValue which is invalid because it does not
    exist in the list of items.
    Parameter name: value

    This seems to me like something strange is happening when the page is being
    rendered because there is no error during the Page_Load. It happens
    immediately afterward.

    Additional ideas and/or suggestions are welcome and greatly appreciated.
    Thank you.
    Bryan Stauffer, Jul 7, 2006
  6. I have found the source of my problem and it was a typographical error in a
    literal. I'm very sorry to have missed this. Thanks again to CaffieneRush
    for pointing out the Items Collection FindByText() method!

    Bryan Stauffer, Jul 7, 2006
  7. Bryan Stauffer

    CaffieneRush Guest

    Glad to hear that you tracked down your problem.


    CaffieneRush, Jul 8, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.