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.
http://www.experts-exchange.com/Web/WebDevSoftware/DreamWeaver/Q_21798396.html
Swn-Y-MorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

smaccariCommented:
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")%>>
0
Swn-Y-MorAuthor Commented:
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.
0
smaccariCommented:
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.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Swn-Y-MorAuthor Commented:
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?
0
smaccariCommented:
Ok send me your code. You will find my email by sneding it at my member name @ gmail.com.
0
Swn-Y-MorAuthor Commented:
OK, code sent.
Thanks
0
smaccariCommented:
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.

 
0
smaccariCommented:
Ok i looked at your code but i can't see what is not working, all seems to be fine.
Does the page sets the correct value to your DB ItemYesNo Field if the checkbox is checked, or not ?

I do not know well Access and the Yes/No field, and what kind of value it returns (strings "Yes"/"No", integers 1/0, booleans True/False ?).
Can you just output the value of rsItem("ItemYesNo") (before your input for example) to see what value it returns ? (just add <%=rsItem("ItemYesNo")%>)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Swn-Y-MorAuthor Commented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.