how to get a checkbox checked when loading the page?

Discussion in 'ASP General' started by Kevin, Oct 29, 2005.

  1. Kevin

    Kevin Guest

    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
    Kevin, Oct 29, 2005
    #1
    1. Advertising

  2. Kevin

    McKirahan Guest

    "Kevin" <> wrote in message
    news:...
    > 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.
    McKirahan, Oct 29, 2005
    #2
    1. Advertising

  3. 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"
    Bob Barrows [MVP], Oct 29, 2005
    #3
  4. Kevin

    Kevin Guest

    Thanks both


    "Bob Barrows [MVP]" <> wrote in message
    news:%...
    > 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"
    >
    >
    Kevin, Oct 29, 2005
    #4
  5. Kevin

    PJones Guest

    http://www.powerasp.com/content/code-snippets/forms-populate-check-box.asp


    "Kevin" <> wrote in message
    news:...
    > 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
    >
    >
    PJones, Nov 27, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. yurps
    Replies:
    1
    Views:
    6,853
    rajeshthangarasu1983
    Feb 29, 2008
  2. dx
    Replies:
    1
    Views:
    552
    Jc Morin
    Feb 25, 2005
  3. John E
    Replies:
    2
    Views:
    1,153
    Lasse Reichstein Nielsen
    Jul 3, 2003
  4. John E
    Replies:
    2
    Views:
    300
    Lasse Reichstein Nielsen
    Jul 3, 2003
  5. randy
    Replies:
    13
    Views:
    422
    Tad McClellan
    Nov 1, 2003
Loading...

Share This Page