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"