• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 333
  • Last Modified:

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
0
Swn-Y-Mor
Asked:
Swn-Y-Mor
  • 5
  • 4
1 Solution
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now