getting selected value when there is no selected value in asp.net
So i have the code in my page below which is displayed for the first time to collect data from the user, when I don't selected anything, submit the screen and then execute the statement:
If Insurance.SelectedValue Is Nothing Then
it fails this check. It also stores a 'false' in my database, when I
store
Insurance.SelectedValue, tied to a bit field in my database.
How can I find out if there was nothing selected by the user.
A dropdown list will always have something selected.
IF you dont specify the selected value it will just be the last one - which is why you get false entered into the DB. What else do you want to store in the DB anyway?
If you want to leave the DB entry as null then you could add another item like
<asp:ListItem Value="">Please select</asp:ListItem>
then in code behind
If Insurance.SelectedValue !=""
{
store in DB
}
So if the value is blank then dont update the DB.
d2fox
ASKER
wow, I did not know that...this is a radiobutton list, does that make any difference? So it defaults to the LAST one
so can I create a last one that's not visible, tie it to my bit field and have the bit field stay null?
that way I wouldn't need to code checks on each field (I have several like this)
or is it better to code each one?
rajeeshmca
HI d2fox,
u can use the SelectedIndex property of the RadioButtonList....
> A dropdown list will always have something selected.
Only when it is a default dropdown. Setting "size" to more than 1 or using "multiple" will both allow for dropdown lists with no selected value. And of course a dropdown with no options will have no selected value.
> IF you dont specify the selected value it will just be the last one
No. It will by default select the first option.
> How can I find out if there was nothing selected by the user.
I would check the Request.Form. If Request.Form("yourradiobutton") has no value, nothing was checked by the user. But I realize that this depends on avoiding webforms.
Si_Hibbard
You can set which listitem you want selected by using Selected="True" and maybe set the value to null, but I am not sure if it will insert a 'null' value like you want without doing some codebehind check, you could try this but I havent had a chance to try myself.
Where the field that I am updating with the @Insurance parameter is a bit field.
but this doesn't work, as if there is nothing selected yet, it stores a false. How can i do ths?
rajeeshmca
what do you exactly want to do??? if nothing is selected what should be stored???
chrisgreaves
Hi,
If you choose to use a checkbox, then it is this...
dim InsuranceSelected as boolean
if checkbox1.checked then
InsuranceSelected = true
else
InsuranceSelected = false
end if
or, more simply...
dim InsuranceSelected as boolean = checkbox1.checked
then
.Add(New SqlParameter("@Insurance", InsuranceSelected))
Chris
fajeeshmca, if there is nothing selected, I want to store a null
d2fox
ASKER
so if it's 'true' selected, i want to store 'true', if it's 'false' I want to store 'false' and if there is nothing selected I want to store a null (or nothing).
IF you dont specify the selected value it will just be the last one - which is why you get false entered into the DB. What else do you want to store in the DB anyway?
If you want to leave the DB entry as null then you could add another item like
<asp:ListItem Value="">Please select</asp:ListItem>
then in code behind
If Insurance.SelectedValue !=""
{
store in DB
}
So if the value is blank then dont update the DB.