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

    this.SetLoanTypes(propertyType);

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

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



    protected void SetLoanTypes(string PropertyType)
    {
    XmlDocument xml = new XmlDocument();
    switch (PropertyType)
    {
    case "Commercial":
    xml.Load(MapPath(@"~\data\commercial_loan_types.xml"));
    break;
    case "Residential":
    xml.Load(MapPath(@"~\data\residential_loan_types.xml"));
    break;
    default:
    throw new Exception("Invalid PropertyType in SetLoanTypes()");
    }
    XmlNodeList nlLoanTypes = xml.SelectNodes("loan_types/loan_type");
    this.drpLoanType.Items.Clear();
    foreach (XmlNode n in nlLoanTypes)
    {
    this.drpLoanType.Items.Add(new
    ListItem(n.Attributes["display_member"].Value,
    n.Attributes["value_member"].Value));
    }
    }
     
    Bryan Stauffer, Jul 6, 2006
    #1
    1. Advertising

  2. Bryan Stauffer

    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.
    <quoteMSDNExample>
    ' Perform this operation in a try-catch block in case the item
    is not found.
    Try
    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
    </quoteMSDNExample>

    Personally, I avoid the SelectedValue property when selecting an item
    in the dropdown. Instead, I find the item directly before selecting it.
    <quoteMSDNExample>
    ' Selects the item whose text is Apples
    ListBox1.Items.FindByText("Apples")
    If Not li Is Nothing Then
    li.Selected = True
    End If
    </quoteMSDNExample>

    Regards

    Bryan Stauffer wrote:
    > 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";
    >
    > this.SetLoanTypes(propertyType);
    >
    > switch (cart.Order.PropertyType)
    > {
    > case PropertyType.Commercial:
    > this.drpPropertyType.SelectedValue = "Commerical";
    > break;
    > case PropertyType.Residential:
    > this.drpPropertyType.SelectedValue = "Residential";
    > break;
    > }
    >
    > this.drpLoanType.SelectedValue = cart.Order.LoanType;
    > }
    >
    >
    >
    > protected void SetLoanTypes(string PropertyType)
    > {
    > XmlDocument xml = new XmlDocument();
    > switch (PropertyType)
    > {
    > case "Commercial":
    > xml.Load(MapPath(@"~\data\commercial_loan_types.xml"));
    > break;
    > case "Residential":
    > xml.Load(MapPath(@"~\data\residential_loan_types.xml"));
    > break;
    > default:
    > throw new Exception("Invalid PropertyType in SetLoanTypes()");
    > }
    > XmlNodeList nlLoanTypes = xml.SelectNodes("loan_types/loan_type");
    > this.drpLoanType.Items.Clear();
    > foreach (XmlNode n in nlLoanTypes)
    > {
    > this.drpLoanType.Items.Add(new
    > ListItem(n.Attributes["display_member"].Value,
    > n.Attributes["value_member"].Value));
    > }
    > }
     
    , Jul 6, 2006
    #2
    1. Advertising

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

    <> wrote in message
    news:...
    > 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.
    > <quoteMSDNExample>
    > ' Perform this operation in a try-catch block in case the item
    > is not found.
    > Try
    > 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
    > </quoteMSDNExample>
    >
    > Personally, I avoid the SelectedValue property when selecting an item
    > in the dropdown. Instead, I find the item directly before selecting it.
    > <quoteMSDNExample>
    > ' Selects the item whose text is Apples
    > ListBox1.Items.FindByText("Apples")
    > If Not li Is Nothing Then
    > li.Selected = True
    > End If
    > </quoteMSDNExample>
    >
    > Regards
    >
    > Bryan Stauffer wrote:
    >> 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";
    >>
    >> this.SetLoanTypes(propertyType);
    >>
    >> switch (cart.Order.PropertyType)
    >> {
    >> case PropertyType.Commercial:
    >> this.drpPropertyType.SelectedValue = "Commerical";
    >> break;
    >> case PropertyType.Residential:
    >> this.drpPropertyType.SelectedValue = "Residential";
    >> break;
    >> }
    >>
    >> this.drpLoanType.SelectedValue = cart.Order.LoanType;
    >> }
    >>
    >>
    >>
    >> protected void SetLoanTypes(string PropertyType)
    >> {
    >> XmlDocument xml = new XmlDocument();
    >> switch (PropertyType)
    >> {
    >> case "Commercial":
    >> xml.Load(MapPath(@"~\data\commercial_loan_types.xml"));
    >> break;
    >> case "Residential":
    >> xml.Load(MapPath(@"~\data\residential_loan_types.xml"));
    >> break;
    >> default:
    >> throw new Exception("Invalid PropertyType in
    >> SetLoanTypes()");
    >> }
    >> XmlNodeList nlLoanTypes = xml.SelectNodes("loan_types/loan_type");
    >> this.drpLoanType.Items.Clear();
    >> foreach (XmlNode n in nlLoanTypes)
    >> {
    >> this.drpLoanType.Items.Add(new
    >> ListItem(n.Attributes["display_member"].Value,
    >> n.Attributes["value_member"].Value));
    >> }
    >> }

    >
     
    Bryan Stauffer, Jul 7, 2006
    #3
  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?

    <> wrote in message
    news:...
    > 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.
    > <quoteMSDNExample>
    > ' Perform this operation in a try-catch block in case the item
    > is not found.
    > Try
    > 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
    > </quoteMSDNExample>
    >
    > Personally, I avoid the SelectedValue property when selecting an item
    > in the dropdown. Instead, I find the item directly before selecting it.
    > <quoteMSDNExample>
    > ' Selects the item whose text is Apples
    > ListBox1.Items.FindByText("Apples")
    > If Not li Is Nothing Then
    > li.Selected = True
    > End If
    > </quoteMSDNExample>
    >
    > Regards
    >
    > Bryan Stauffer wrote:
    >> 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";
    >>
    >> this.SetLoanTypes(propertyType);
    >>
    >> switch (cart.Order.PropertyType)
    >> {
    >> case PropertyType.Commercial:
    >> this.drpPropertyType.SelectedValue = "Commerical";
    >> break;
    >> case PropertyType.Residential:
    >> this.drpPropertyType.SelectedValue = "Residential";
    >> break;
    >> }
    >>
    >> this.drpLoanType.SelectedValue = cart.Order.LoanType;
    >> }
    >>
    >>
    >>
    >> protected void SetLoanTypes(string PropertyType)
    >> {
    >> XmlDocument xml = new XmlDocument();
    >> switch (PropertyType)
    >> {
    >> case "Commercial":
    >> xml.Load(MapPath(@"~\data\commercial_loan_types.xml"));
    >> break;
    >> case "Residential":
    >> xml.Load(MapPath(@"~\data\residential_loan_types.xml"));
    >> break;
    >> default:
    >> throw new Exception("Invalid PropertyType in
    >> SetLoanTypes()");
    >> }
    >> XmlNodeList nlLoanTypes = xml.SelectNodes("loan_types/loan_type");
    >> this.drpLoanType.Items.Clear();
    >> foreach (XmlNode n in nlLoanTypes)
    >> {
    >> this.drpLoanType.Items.Add(new
    >> ListItem(n.Attributes["display_member"].Value,
    >> n.Attributes["value_member"].Value));
    >> }
    >> }

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

    try
    {
    ListItem li = this.drpLoanType.Items.FindByText(cart.Order.LoanType);
    if (li != null)
    li.Selected = true;
    }
    catch (Exception ex)
    {
    Response.Write(ex.Message);
    }

    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
    #5
  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!

    <> wrote in message
    news:...
    > 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.
    > <quoteMSDNExample>
    > ' Perform this operation in a try-catch block in case the item
    > is not found.
    > Try
    > 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
    > </quoteMSDNExample>
    >
    > Personally, I avoid the SelectedValue property when selecting an item
    > in the dropdown. Instead, I find the item directly before selecting it.
    > <quoteMSDNExample>
    > ' Selects the item whose text is Apples
    > ListBox1.Items.FindByText("Apples")
    > If Not li Is Nothing Then
    > li.Selected = True
    > End If
    > </quoteMSDNExample>
    >
    > Regards
    >
    > Bryan Stauffer wrote:
    >> 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";
    >>
    >> this.SetLoanTypes(propertyType);
    >>
    >> switch (cart.Order.PropertyType)
    >> {
    >> case PropertyType.Commercial:
    >> this.drpPropertyType.SelectedValue = "Commerical";
    >> break;
    >> case PropertyType.Residential:
    >> this.drpPropertyType.SelectedValue = "Residential";
    >> break;
    >> }
    >>
    >> this.drpLoanType.SelectedValue = cart.Order.LoanType;
    >> }
    >>
    >>
    >>
    >> protected void SetLoanTypes(string PropertyType)
    >> {
    >> XmlDocument xml = new XmlDocument();
    >> switch (PropertyType)
    >> {
    >> case "Commercial":
    >> xml.Load(MapPath(@"~\data\commercial_loan_types.xml"));
    >> break;
    >> case "Residential":
    >> xml.Load(MapPath(@"~\data\residential_loan_types.xml"));
    >> break;
    >> default:
    >> throw new Exception("Invalid PropertyType in
    >> SetLoanTypes()");
    >> }
    >> XmlNodeList nlLoanTypes = xml.SelectNodes("loan_types/loan_type");
    >> this.drpLoanType.Items.Clear();
    >> foreach (XmlNode n in nlLoanTypes)
    >> {
    >> this.drpLoanType.Items.Add(new
    >> ListItem(n.Attributes["display_member"].Value,
    >> n.Attributes["value_member"].Value));
    >> }
    >> }

    >
     
    Bryan Stauffer, Jul 7, 2006
    #6
  7. Bryan Stauffer

    Guest

    Glad to hear that you tracked down your problem.

    Regards,
    Andy

    Bryan Stauffer wrote:
    > 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!
    >
    > <> wrote in message
    > news:...
    > > 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.
    > > <quoteMSDNExample>
    > > ' Perform this operation in a try-catch block in case the item
    > > is not found.
    > > Try
    > > 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
    > > </quoteMSDNExample>
    > >
    > > Personally, I avoid the SelectedValue property when selecting an item
    > > in the dropdown. Instead, I find the item directly before selecting it.
    > > <quoteMSDNExample>
    > > ' Selects the item whose text is Apples
    > > ListBox1.Items.FindByText("Apples")
    > > If Not li Is Nothing Then
    > > li.Selected = True
    > > End If
    > > </quoteMSDNExample>
    > >
    > > Regards
    > >
    > > Bryan Stauffer wrote:
    > >> 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";
    > >>
    > >> this.SetLoanTypes(propertyType);
    > >>
    > >> switch (cart.Order.PropertyType)
    > >> {
    > >> case PropertyType.Commercial:
    > >> this.drpPropertyType.SelectedValue = "Commerical";
    > >> break;
    > >> case PropertyType.Residential:
    > >> this.drpPropertyType.SelectedValue = "Residential";
    > >> break;
    > >> }
    > >>
    > >> this.drpLoanType.SelectedValue = cart.Order.LoanType;
    > >> }
    > >>
    > >>
    > >>
    > >> protected void SetLoanTypes(string PropertyType)
    > >> {
    > >> XmlDocument xml = new XmlDocument();
    > >> switch (PropertyType)
    > >> {
    > >> case "Commercial":
    > >> xml.Load(MapPath(@"~\data\commercial_loan_types.xml"));
    > >> break;
    > >> case "Residential":
    > >> xml.Load(MapPath(@"~\data\residential_loan_types.xml"));
    > >> break;
    > >> default:
    > >> throw new Exception("Invalid PropertyType in
    > >> SetLoanTypes()");
    > >> }
    > >> XmlNodeList nlLoanTypes = xml.SelectNodes("loan_types/loan_type");
    > >> this.drpLoanType.Items.Clear();
    > >> foreach (XmlNode n in nlLoanTypes)
    > >> {
    > >> this.drpLoanType.Items.Add(new
    > >> ListItem(n.Attributes["display_member"].Value,
    > >> n.Attributes["value_member"].Value));
    > >> }
    > >> }

    > >
     
    , Jul 8, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmFjaw==?=

    Dropdown list - SelectedIndexChanged event doesn't fire

    =?Utf-8?B?SmFjaw==?=, Mar 14, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    3,510
    =?Utf-8?B?SmFjaw==?=
    Mar 14, 2005
  2. vishnu
    Replies:
    1
    Views:
    1,232
    Patrick.O.Ige
    Mar 25, 2006
  3. Mike
    Replies:
    1
    Views:
    825
  4. acadam
    Replies:
    0
    Views:
    767
    acadam
    Dec 27, 2006
  5. Andries

    I know, I know, I don't know

    Andries, Apr 23, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    254
    Gregory Toomey
    Apr 23, 2004
Loading...

Share This Page