473,508 Members | 2,289 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to get a checkbox checked when loading the page?

Hi,

I have a input with type checkbox. I want it to be automatically checked if
the value from the corresponding field in the database is also checked.
I tried this:
<%
set objdc = Server.CreateObject("ADODB.Connection")
etc ...
sql="select maa13 from mytable"
dim rsArray
Set rs = objdc.execute(sql)
if not rs.eof then
rsArray = rs.GetRows()
rec = UBound(rsArray, 2) + 1
rs.movefirst
....
%>
<html><body>
<input id="in100" name="maa13" type="checkbox"
value="<%=rs.Fields("maa13").Value%>>
.....

but nothing happens!
I can do this in Javascript like:
<script type="text/javascript">
if (<%=rs.Fields("maa13").Value%>="true")
{ document.getElementById("in100").checked=true}
.....

but it seems me possible to do it without scripting, or not?

Thanks
Kevin
Oct 29 '05 #1
4 9786
"Kevin" <ks****@smart.ca> wrote in message
news:OD*************@TK2MSFTNGP15.phx.gbl...
Hi,

I have a input with type checkbox. I want it to be automatically checked if the value from the corresponding field in the database is also checked.
I tried this:
<%
set objdc = Server.CreateObject("ADODB.Connection")
etc ...
sql="select maa13 from mytable"
dim rsArray
Set rs = objdc.execute(sql)
if not rs.eof then
rsArray = rs.GetRows()
rec = UBound(rsArray, 2) + 1
rs.movefirst
...
%>
<html><body>
<input id="in100" name="maa13" type="checkbox"
value="<%=rs.Fields("maa13").Value%>>
....

but nothing happens!
I can do this in Javascript like:
<script type="text/javascript">
if (<%=rs.Fields("maa13").Value%>="true")
{ document.getElementById("in100").checked=true}
....

but it seems me possible to do it without scripting, or not?

Thanks
Kevin


Try:

<% If rs.Fields("maa13").Value Then %>
<input id="in100" name="maa13" type="checkbox" value="" checked>
<% Else %>
<input id="in100" name="maa13" type="checkbox" value="">
<% End If %>

Or:

<% Dim strCHK
strCHK = ""
If rs.Fields("maa13").Value Then strCHK = "checked"
%>
<input id="in100" name="maa13" type="checkbox" value="" <%=strCHK%>>
Not sure what the value of value= should be.
Oct 29 '05 #2
Kevin wrote:
Hi,

I have a input with type checkbox. I want it to be automatically
checked if the value from the corresponding field in the database is
also checked.
I tried this:
<%
set objdc = Server.CreateObject("ADODB.Connection")
etc ...
sql="select maa13 from mytable"
dim rsArray
Set rs = objdc.execute(sql)
if not rs.eof then
rsArray = rs.GetRows()
rec = UBound(rsArray, 2) + 1
rs.movefirst
Why did you issue the movefirst? Aren't you planning to close and destroy
the recodset and connection at this point? You should ...
...
%>
<html><body>
<input id="in100" name="maa13" type="checkbox"
value="<%=rs.Fields("maa13").Value%>>
You have an array containing your data. Why are you still using the
recordset?
....

but nothing happens!
Why should it? All you've done is set the value of the the checkbox, ie, the
value that will be contained in the Request if the checkbox is checked. You
haven't set the "checked" property.
I can do this in Javascript like:
<script type="text/javascript">
if (<%=rs.Fields("maa13").Value%>="true")
{ document.getElementById("in100").checked=true}
....
Absolutely. just add the word "checked" into the checkboxes tag if you want
it to be checked (you want to look at the first record in the array,
correct?):

<input id="in100" name="maa13" type="checkbox"
value= "true"
<%if rsArray(0,0) = "true" then "checked" %>


Personally I dislike mixing server-side code in my html. i would prefer:

<%
'same as above, only close and destroy ADO objects after
'creating the array
'Then:

dim sCheckboxHTML
sCheckboxHTML="<input id=""in100"" name=""maa13"" " & _
"type=""checkbox"" value = ""true"""
If IsArray(rsArray) then
'array contains records
if rsArray(0,0) = "true" then
sCheckboxHTML=sCheckboxHTML & " checked"
end if
end if
sCheckboxHTML=sCheckboxHTML & ">"
....
%>
<html><body>
<%=sCheckboxHTML%>

etc.
HTH,
Bob Barrows
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Oct 29 '05 #3
Thanks both
"Bob Barrows [MVP]" <re******@NOyahoo.SPAMcom> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Kevin wrote:
Hi,

I have a input with type checkbox. I want it to be automatically
checked if the value from the corresponding field in the database is
also checked.
I tried this:
<%
set objdc = Server.CreateObject("ADODB.Connection")
etc ...
sql="select maa13 from mytable"
dim rsArray
Set rs = objdc.execute(sql)
if not rs.eof then
rsArray = rs.GetRows()
rec = UBound(rsArray, 2) + 1
rs.movefirst
Why did you issue the movefirst? Aren't you planning to close and destroy
the recodset and connection at this point? You should ...
...
%>
<html><body>
<input id="in100" name="maa13" type="checkbox"
value="<%=rs.Fields("maa13").Value%>>


You have an array containing your data. Why are you still using the
recordset?
....

but nothing happens!


Why should it? All you've done is set the value of the the checkbox, ie,

the value that will be contained in the Request if the checkbox is checked. You haven't set the "checked" property.
I can do this in Javascript like:
<script type="text/javascript">
if (<%=rs.Fields("maa13").Value%>="true")
{ document.getElementById("in100").checked=true}
....
Absolutely. just add the word "checked" into the checkboxes tag if you

want it to be checked (you want to look at the first record in the array,
correct?):

<input id="in100" name="maa13" type="checkbox"
value= "true"
<%if rsArray(0,0) = "true" then "checked" %>


Personally I dislike mixing server-side code in my html. i would prefer:

<%
'same as above, only close and destroy ADO objects after
'creating the array
'Then:

dim sCheckboxHTML
sCheckboxHTML="<input id=""in100"" name=""maa13"" " & _
"type=""checkbox"" value = ""true"""
If IsArray(rsArray) then
'array contains records
if rsArray(0,0) = "true" then
sCheckboxHTML=sCheckboxHTML & " checked"
end if
end if
sCheckboxHTML=sCheckboxHTML & ">"
...
%>
<html><body>
<%=sCheckboxHTML%>

etc.
HTH,
Bob Barrows
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

Oct 29 '05 #4
http://www.powerasp.com/content/code...-check-box.asp
"Kevin" <ks****@smart.ca> wrote in message
news:OD*************@TK2MSFTNGP15.phx.gbl...
Hi,

I have a input with type checkbox. I want it to be automatically checked
if
the value from the corresponding field in the database is also checked.
I tried this:
<%
set objdc = Server.CreateObject("ADODB.Connection")
etc ...
sql="select maa13 from mytable"
dim rsArray
Set rs = objdc.execute(sql)
if not rs.eof then
rsArray = rs.GetRows()
rec = UBound(rsArray, 2) + 1
rs.movefirst
...
%>
<html><body>
<input id="in100" name="maa13" type="checkbox"
value="<%=rs.Fields("maa13").Value%>>
....

but nothing happens!
I can do this in Javascript like:
<script type="text/javascript">
if (<%=rs.Fields("maa13").Value%>="true")
{ document.getElementById("in100").checked=true}
....

but it seems me possible to do it without scripting, or not?

Thanks
Kevin

Nov 27 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4610
by: Jack | last post by:
Hi, I have a checkbox the value which goes to a database via a asp page that builds the sql string. In the front end asp page, the checkbox code is written as follows: <i><input...
2
6545
by: Asha | last post by:
greetings, i'm loading a datagrid and my gird has checkbox, i want these checkboxex to be check based on the value from the db? can someone please show me the code for it? thanks
2
1454
by: JIM | last post by:
Hello, I would like to solve the following problem : I've a web site with ex. 5 checkboxes Each checkbox contains the name of a certain task ex. asp:Checkbox 1: Task 1 asp:Checkbox 2: ...
1
4094
by: Kevin R | last post by:
This is one of the weirdest problems I have ever run into. I have had to trim down a bunch of code to give a sample that is more easily readable by those who will view this. Here is the problem:...
2
8490
by: ColliersWNC | last post by:
I know there is probably a simple solution to this, but I haven't figured it out yet or seen it else where. Imagine you had a simple web page that looked like the following: <html> <input...
10
5180
by: rn5a | last post by:
All the rows in a DataGrid, including the Header, are accompanied with a CheckBox. I want that when the CheckBox in the Header is checked, then all the CheckBoxes should automatically get checked....
11
4278
by: =?Utf-8?B?UGFyYWcgR2Fpa3dhZA==?= | last post by:
Hi All, I have a large recordset to be displayed on a ASP 3.0 page. I am using recordset paging for this. For the next and previous link i am passing href as <a href=<Page URl>?page=<%=...
4
4428
by: Charlotte | last post by:
Hi, I have a problem with a ASP-script, can somewone help me ? here is what I've got: mypage.asp : .... code ... <%
0
7120
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7380
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5050
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1553
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.