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

B

Bryan Stauffer

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));
}
}
 
C

CaffieneRush

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 said:
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));
}
}
 
B

Bryan Stauffer

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

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 said:
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));
}
}
 
B

Bryan Stauffer

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?

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 said:
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));
}
}
 
B

Bryan Stauffer

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

Bryan Stauffer

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!

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 said:
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));
}
}
 
C

CaffieneRush

Glad to hear that you tracked down your problem.

Regards,
Andy

Bryan said:
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!

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 said:
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));
}
}
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top