how to get a checkbox checked when loading the page?

K

Kevin

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
 
M

McKirahan

Kevin said:
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.
 
B

Bob Barrows [MVP]

Kevin said:
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"

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
 
K

Kevin

Thanks both


Bob Barrows said:
Why did you issue the movefirst? Aren't you planning to close and destroy
the recodset and connection at this point? You should ...


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


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.

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"

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"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top