Link to home
Start Free TrialLog in
Avatar of Swn-Y-Mor
Swn-Y-Mor

asked on

Bind data to a checkbox

Classic ASP, VBScript and Access database

I am creating an 'Edit Item' page, and I now want to bind ItemYesNo value to a checkbox, so that checkbox displays correct value stored in db.

Here's what I have so far....

<input name="ItemYesNo" type="checkbox" id="ItemYesNo" value="<%If (CStr((rsItem.Fields.Item("ItemYesNo").Value)) = CStr("1")) Then Response.Write("checked")%>
">

but it doesn't work. Help


I already posted this question herein Dreamweaver section, but thought here might be more suitable.
https://www.experts-exchange.com/questions/21798396/Bind-data-to-checkbox.html
Avatar of smaccari
smaccari

What do you want to do exactly ? Check the checkbox or set its value to your ItemYesNo value ? Maybe both ?

Here is if you want to do both :

<input name="ItemYesNo" type="checkbox" id="ItemYesNo" value="<%=CStr(rsItem.Fields.Item("ItemYesNo").Value)%>" <%If (CStr((rsItem.Fields.Item("ItemYesNo").Value)) = CStr("1")) Then Response.Write("checked")%>>
Avatar of Swn-Y-Mor

ASKER

I am trying to set the Checkbox's value to the value stored in my database.

I have a list of Items at a normal price. Every now and then, I need a clearout, and put some items on sale. I want to be able to tick a checkbox to show that that item is on sale.
This I can do easily.
I now need to edit the details of some items, and have a 'Edit Details' form containing a checkbox (which should be ticked or unticked depending if on sale)
all my checkboxes are unticked each time, even though i know that some should be.
The code i provided will :
- set the value of the ItemYesNo input to your ItemYesNo current value
- check the checkbox if the value is 1

Now if by "editing" you mean that the user edit and can change the setting of the item to "on sale" or "not on sale" then you should always put 1 in the value of your input checkbox, and just do the server test to know if on edit entering the item is already on sale or not (that is if your checkbox should be check or not by default).
In any case, when the user validates the form :
- if the checkbox is checked (whether the user checked it or it was checked by default and the user did not change its state) you will have the value 1 in the ItemYesNo parameter of the QueryString (or the Form if method POST).
- If it is not checked, no ItemYesNo parameter will be passed to your validation page through QueryString (or Form)

<input name="ItemYesNo" type="checkbox" id="ItemYesNo" value="1" <%If (CStr((rsItem.Fields.Item("ItemYesNo").Value)) = CStr("1")) Then Response.Write("checked")%>>



Hope this help.
Tried your code, but still no joy.
Yes, by editing, I mean that my user can decide what is 'On sale' and what is 'Not on sale'

My access database field is a Yes/No datatype

Would it help if I email you the page code?
Ok send me your code. You will find my email by sneding it at my member name @ gmail.com.
OK, code sent.
Thanks
Ok i will check tomorrow morning (it's 1 AM here now ;)
But first i just took a rapid look on your edit page generated source code.
The value of your DB ItemYesNo is Yes in case where it is in sale (you said it but i did not catch it ;)
So for your edit page, to show the checkbox in the good state, the input code should be :

<input name="ItemYesNo" type="checkbox" id="ItemYesNo" value="Yes" <%If (CStr((rsItem.Fields.Item("ItemYesNo").Value)) = CStr("Yes")) Then Response.Write("checked")%>>

Your Update code for your DB should then put "Yes" in the case where you have something in the Request.Form("ItemYesNo"). Else it should put "No" in the field.

 
ASKER CERTIFIED SOLUTION
Avatar of smaccari
smaccari

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Value returned using <%=rsItem("ItemYesNo")%> is True

Amended your code above to give the following :-

<input name="ItemYesNo" type="checkbox" id="ItemYesNo" value="True" <%If (CStr((rsItem.Fields.Item("ItemYesNo").Value)) = CStr("True")) Then Response.Write("checked")%>>

And it worked.............Thank You very much!
I'll award you the points straight away