|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Dropdown List doesn't know what's in it's listconfigured 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)); } } 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: Show quoteHide quote > 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)); > } > } Thank you for the ideas! I will try them immediately and let you know how
it worked. Thanks again. <CaffieneR***@gmail.com> wrote in message Show quoteHide quote news:1152222043.272198.319280@s53g2000cws.googlegroups.com... > 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)); >> } >> } > 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? <CaffieneR***@gmail.com> wrote in message Show quoteHide quote news:1152222043.272198.319280@s53g2000cws.googlegroups.com... > 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)); >> } >> } > 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. 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! <CaffieneR***@gmail.com> wrote in message Show quoteHide quote news:1152222043.272198.319280@s53g2000cws.googlegroups.com... > 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)); >> } >> } > Glad to hear that you tracked down your problem.
Regards, Andy Bryan Stauffer wrote: Show quoteHide quote > 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! > > <CaffieneR***@gmail.com> wrote in message > news:1152222043.272198.319280@s53g2000cws.googlegroups.com... > > 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)); > >> } > >> } > >
dropdown list post back not working
Treeview SelectedNodeStyle Problem Passing values to user controls - naughty problem Why it doesn't work? ObjectDataSource ControlParameters Problem with ASP.NET wizard hiding / removing steps problem with hidden field from a custom control in a form Can't access webpage's controls from the code-behind file after putting it in a <asp:LoginView/> Problem with spaces in values of the DataTextField property of datagrid control Custom Templated CompositeControl/CompositeControlDesigner |
|||||||||||||||||||||||