|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
SelectedValue which is invalidWhat is the best way to handle null values in dropdownlists?
Why is the dropdownlist generating the error "System.ArgumentOutOfRangeException: 'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items." on null values? Should it not be possible to have an empty dropdownlist field? Thanks Bart I would always check before assigning a value to the
DropDownList.SelectedValue like this: ListItem li = ddl.Items.FindByValue(myValue); ddl.ClearSelection(); if (li != null) //it means the value is found in the list { li.Selected = true; //select the item that was found } Show quoteHide quote "Bart" wrote: > What is the best way to handle null values in dropdownlists? > Why is the dropdownlist generating the error > "System.ArgumentOutOfRangeException: 'DropDownList1' has a SelectedValue > which is invalid because it does not exist in the list of items." on null > values? > Should it not be possible to have an empty dropdownlist field? > > Thanks > Bart > > Hi Phillip,
Sorry for the late answer. I'm using the default ASP.NET 2.0 databinding features. Isn't there another way to handle this exeption? The example you gave has a MyValue parameter, but how can I get the value, lets say in the databind event of the dropdown ? Thanks Show quoteHide quote "Phillip Williams" wrote: > I would always check before assigning a value to the > DropDownList.SelectedValue like this: > > ListItem li = ddl.Items.FindByValue(myValue); > ddl.ClearSelection(); > if (li != null) //it means the value is found in the list > { > li.Selected = true; //select the item that was found > } > > -- > HTH, > Phillip Williams > http://www.societopia.net > http://www.webswapp.com > > > "Bart" wrote: > > > What is the best way to handle null values in dropdownlists? > > Why is the dropdownlist generating the error > > "System.ArgumentOutOfRangeException: 'DropDownList1' has a SelectedValue > > which is invalid because it does not exist in the list of items." on null > > values? > > Should it not be possible to have an empty dropdownlist field? > > > > Thanks > > Bart > > > > The problem with binding a datafield to a dropdownlist is that unless you
populate the dropdownlist with all possible values that can be found in that databound field then you will get an error that the databound value (that you are attempting to set as the selected value of the dropdown list) does not exist. In other words if your list is not being updated dynamically with all possible values then you might consider the following: 1- In one-way databinding: instead of setting the SelectedValue of the list as Eval("fieldname"), handle the DataBound event of the dropdownlist to set the selected value as I did in this demo http://www.webswapp.com/CodeSamples/aspnet20/FormView1.aspx 2- In 2-way databinding instead of using Bind, handle the container (let say a GridView) RowUpdating event to set the selected value of the list if found. (look at this thread http://groups.google.ca/group/microsoft.public.dotnet.framework.aspnet/msg/0 c3bec192c3a79dd) Show quoteHide quote "Bart" <bartVDA@community.nospam> wrote in message news:E8391911-8A61-4E20-8FF8-81743233FD09@microsoft.com... > Hi Phillip, > > Sorry for the late answer. > > I'm using the default ASP.NET 2.0 databinding features. > Isn't there another way to handle this exeption? > > The example you gave has a MyValue parameter, but how can I get the value, > lets say in the databind event of the dropdown ? > > Thanks > > "Phillip Williams" wrote: > > > I would always check before assigning a value to the > > DropDownList.SelectedValue like this: > > > > ListItem li = ddl.Items.FindByValue(myValue); > > ddl.ClearSelection(); > > if (li != null) //it means the value is found in the list > > { > > li.Selected = true; //select the item that was found > > } > > > > -- > > HTH, > > Phillip Williams > > http://www.societopia.net > > http://www.webswapp.com > > > > > > "Bart" wrote: > > > > > What is the best way to handle null values in dropdownlists? > > > Why is the dropdownlist generating the error > > > "System.ArgumentOutOfRangeException: 'DropDownList1' has a SelectedValue > > > which is invalid because it does not exist in the list of items." on null > > > values? > > > Should it not be possible to have an empty dropdownlist field? > > > > > > Thanks > > > Bart > > > > > > Thanks, solution 1 did the trick
Show quoteHide quote "msnews.microsoft.com" wrote: > The problem with binding a datafield to a dropdownlist is that unless you > populate the dropdownlist with all possible values that can be found in that > databound field then you will get an error that the databound value (that > you are attempting to set as the selected value of the dropdown list) does > not exist. > > > > In other words if your list is not being updated dynamically with all > possible values then you might consider the following: > > 1- In one-way databinding: instead of setting the SelectedValue of the > list as Eval("fieldname"), handle the DataBound event of the dropdownlist to > set the selected value as I did in this demo > http://www.webswapp.com/CodeSamples/aspnet20/FormView1.aspx > > 2- In 2-way databinding instead of using Bind, handle the container (let > say a GridView) RowUpdating event to set the selected value of the list if > found. (look at this thread > http://groups.google.ca/group/microsoft.public.dotnet.framework.aspnet/msg/0 > c3bec192c3a79dd) > > > -- > HTH, > Phillip Williams > http://www.societopia.net > http://www.webswapp.com > > > "Bart" <bartVDA@community.nospam> wrote in message > news:E8391911-8A61-4E20-8FF8-81743233FD09@microsoft.com... > > Hi Phillip, > > > > Sorry for the late answer. > > > > I'm using the default ASP.NET 2.0 databinding features. > > Isn't there another way to handle this exeption? > > > > The example you gave has a MyValue parameter, but how can I get the value, > > lets say in the databind event of the dropdown ? > > > > Thanks > > > > "Phillip Williams" wrote: > > > > > I would always check before assigning a value to the > > > DropDownList.SelectedValue like this: > > > > > > ListItem li = ddl.Items.FindByValue(myValue); > > > ddl.ClearSelection(); > > > if (li != null) //it means the value is found in the list > > > { > > > li.Selected = true; //select the item that was found > > > } > > > > > > -- > > > HTH, > > > Phillip Williams > > > http://www.societopia.net > > > http://www.webswapp.com > > > > > > > > > "Bart" wrote: > > > > > > > What is the best way to handle null values in dropdownlists? > > > > Why is the dropdownlist generating the error > > > > "System.ArgumentOutOfRangeException: 'DropDownList1' has a > SelectedValue > > > > which is invalid because it does not exist in the list of items." on > null > > > > values? > > > > Should it not be possible to have an empty dropdownlist field? > > > > > > > > Thanks > > > > Bart > > > > > > > > > > > Actually, the easiest way to handle this is to include null values in
your Select statement by using the UNION operator in SQL: Select StateName , StateId >From dbo.States Select '-- Select --' as StateNameUNION ( , NULL as StateId ) Order by StateId That way everything is automatic and no additional coding is required. This way the DropDownList has a item that you can select should you wish to set the value back to null. Bart wrote: Show quoteHide quote > Thanks, solution 1 did the trick > > "msnews.microsoft.com" wrote: > > > The problem with binding a datafield to a dropdownlist is that unless you > > populate the dropdownlist with all possible values that can be found in that > > databound field then you will get an error that the databound value (that > > you are attempting to set as the selected value of the dropdown list) does > > not exist. > > > > > > > > In other words if your list is not being updated dynamically with all > > possible values then you might consider the following: > > > > 1- In one-way databinding: instead of setting the SelectedValue of the > > list as Eval("fieldname"), handle the DataBound event of the dropdownlist to > > set the selected value as I did in this demo > > http://www.webswapp.com/CodeSamples/aspnet20/FormView1.aspx > > > > 2- In 2-way databinding instead of using Bind, handle the container (let > > say a GridView) RowUpdating event to set the selected value of the list if > > found. (look at this thread > > http://groups.google.ca/group/microsoft.public.dotnet.framework.aspnet/msg/0 > > c3bec192c3a79dd) > > > > > > -- > > HTH, > > Phillip Williams > > http://www.societopia.net > > http://www.webswapp.com > > > > > > "Bart" <bartVDA@community.nospam> wrote in message > > news:E8391911-8A61-4E20-8FF8-81743233FD09@microsoft.com... > > > Hi Phillip, > > > > > > Sorry for the late answer. > > > > > > I'm using the default ASP.NET 2.0 databinding features. > > > Isn't there another way to handle this exeption? > > > > > > The example you gave has a MyValue parameter, but how can I get the value, > > > lets say in the databind event of the dropdown ? > > > > > > Thanks > > > > > > "Phillip Williams" wrote: > > > > > > > I would always check before assigning a value to the > > > > DropDownList.SelectedValue like this: > > > > > > > > ListItem li = ddl.Items.FindByValue(myValue); > > > > ddl.ClearSelection(); > > > > if (li != null) //it means the value is found in the list > > > > { > > > > li.Selected = true; //select the item that was found > > > > } > > > > > > > > -- > > > > HTH, > > > > Phillip Williams > > > > http://www.societopia.net > > > > http://www.webswapp.com > > > > > > > > > > > > "Bart" wrote: > > > > > > > > > What is the best way to handle null values in dropdownlists? > > > > > Why is the dropdownlist generating the error > > > > > "System.ArgumentOutOfRangeException: 'DropDownList1' has a > > SelectedValue > > > > > which is invalid because it does not exist in the list of items." on > > null > > > > > values? > > > > > Should it not be possible to have an empty dropdownlist field? > > > > > > > > > > Thanks > > > > > Bart > > > > > > > > > > > > > > > > Thank for you answer.
In my case it is a dropdown with default answers to questions. But depending on rights they can type in answers. So in this case the content of the dropdown is not the union of al the answers. Bart Show quoteHide quote "TylerFree***@gmail.com" wrote: > Actually, the easiest way to handle this is to include null values in > your Select statement by using the UNION operator in SQL: > > Select > StateName > , StateId > >From dbo.States > UNION > ( > Select '-- Select --' as StateName > , NULL as StateId > ) > Order by StateId > > That way everything is automatic and no additional coding is required. > This way the DropDownList has a item that you can select should you > wish to set the value back to null. > > Bart wrote: > > Thanks, solution 1 did the trick > > > > "msnews.microsoft.com" wrote: > > > > > The problem with binding a datafield to a dropdownlist is that unless you > > > populate the dropdownlist with all possible values that can be found in that > > > databound field then you will get an error that the databound value (that > > > you are attempting to set as the selected value of the dropdown list) does > > > not exist. > > > > > > > > > > > > In other words if your list is not being updated dynamically with all > > > possible values then you might consider the following: > > > > > > 1- In one-way databinding: instead of setting the SelectedValue of the > > > list as Eval("fieldname"), handle the DataBound event of the dropdownlist to > > > set the selected value as I did in this demo > > > http://www.webswapp.com/CodeSamples/aspnet20/FormView1.aspx > > > > > > 2- In 2-way databinding instead of using Bind, handle the container (let > > > say a GridView) RowUpdating event to set the selected value of the list if > > > found. (look at this thread > > > http://groups.google.ca/group/microsoft.public.dotnet.framework.aspnet/msg/0 > > > c3bec192c3a79dd) > > > > > > > > > -- > > > HTH, > > > Phillip Williams > > > http://www.societopia.net > > > http://www.webswapp.com > > > > > > > > > "Bart" <bartVDA@community.nospam> wrote in message > > > news:E8391911-8A61-4E20-8FF8-81743233FD09@microsoft.com... > > > > Hi Phillip, > > > > > > > > Sorry for the late answer. > > > > > > > > I'm using the default ASP.NET 2.0 databinding features. > > > > Isn't there another way to handle this exeption? > > > > > > > > The example you gave has a MyValue parameter, but how can I get the value, > > > > lets say in the databind event of the dropdown ? > > > > > > > > Thanks > > > > > > > > "Phillip Williams" wrote: > > > > > > > > > I would always check before assigning a value to the > > > > > DropDownList.SelectedValue like this: > > > > > > > > > > ListItem li = ddl.Items.FindByValue(myValue); > > > > > ddl.ClearSelection(); > > > > > if (li != null) //it means the value is found in the list > > > > > { > > > > > li.Selected = true; //select the item that was found > > > > > } > > > > > > > > > > -- > > > > > HTH, > > > > > Phillip Williams > > > > > http://www.societopia.net > > > > > http://www.webswapp.com > > > > > > > > > > > > > > > "Bart" wrote: > > > > > > > > > > > What is the best way to handle null values in dropdownlists? > > > > > > Why is the dropdownlist generating the error > > > > > > "System.ArgumentOutOfRangeException: 'DropDownList1' has a > > > SelectedValue > > > > > > which is invalid because it does not exist in the list of items." on > > > null > > > > > > values? > > > > > > Should it not be possible to have an empty dropdownlist field? > > > > > > > > > > > > Thanks > > > > > > Bart > > > > > > > > > > > > > > > > > > > > > > > I too am having this problem.
What I have is the drop downlist w/ a DATASOURCE which populates it with "A", "B", "C" I then have a DATABINDING which may return "Z" - which is not in the list so things are failing. Is there a way to add "Z", "Y", "Q" (what ever text the databinding is trying to set the selection to) prior to the error so that value can be successfully selected. I'd like to avoid giving all the options which i could get by using a union of the datasouce ("A","B","C") and the databinding field. Because the list would be quite long. I'm looking for an option like this: protected sub DropDownList1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) if (item_to_be_selected) is not in DropDownList1 then DropDownList1.additem (item_to_be_selected) end if end sub Hopefully someone out there knows how to access the (item_to_be_selected) data - i cant seam to find out how to get that. also it would be nice if this wasn't specific to a perticular DropDownList - so that the sub could be used for all DropDownLists which need this checked. Thanks in advance if you can help! Drew
ITemplate - Dynamic ImageButton in BindLabelColumn for DataList
Getting value from child control of formview Embedding CSS with inline code SmartNavigation Problem/Bug Reports and the ReportViewer Dataset from ObjectDataSource always null? GridView Data Binding at runtime Design Time referencing of Properties GUID and data controls DotNet & SDK install issue |
|||||||||||||||||||||||