value of checked box!?

J

Jerome

Hi,

I've got the following problem:

I'm retrieving data from an SQL Server database (the data itslef was
entered by an ASP form). And on an 'edit form' I want to display a
checked box in the state corresponding to the value saved in the DB. But
somehow that doesn't work!?

SQL tells me the value is 1.
My ASP page tells me it's 'True'??

So I tried it like this:
<input name="checkbox" type="checkbox" value="checkbox" <%If
rsDemande.Fields.Item("lien").Value = 1 Then Response.Write("
checked='checked'") : Response.Write("")%>>

and like this:

<input name="checkbox" type="checkbox" value="checkbox" <%If
cstr(rsDemande.Fields.Item("lien").Value) = "True" Then Response.Write("
checked='checked'") : Response.Write("")%>>

But neither works. The checkbox remains unchecked!?

What am I doing wrong?

Thanks a lot,

Jerome
 
B

Bob Barrows [MVP]

Jerome said:
Hi,

I've got the following problem:

I'm retrieving data from an SQL Server database (the data itslef was
entered by an ASP form). And on an 'edit form' I want to display a
checked box in the state corresponding to the value saved in the DB.
But somehow that doesn't work!?

SQL tells me the value is 1.

What's the datatype? Bit?
My ASP page tells me it's 'True'??

ADO will interpret the data in a Bit column and convert it to a boolean.
So I tried it like this:
<input name="checkbox" type="checkbox" value="checkbox" <%If
rsDemande.Fields.Item("lien").Value = 1 Then Response.Write("
checked='checked'") : Response.Write("")%>>

and like this:

<input name="checkbox" type="checkbox" value="checkbox" <%If
cstr(rsDemande.Fields.Item("lien").Value) = "True" Then
Response.Write(" checked='checked'") : Response.Write("")%>>

But neither works. The checkbox remains unchecked!?

If rsDemande("lien") = true then ...

Bob Barrows
 
E

Evertjan.

Bob Barrows [MVP] wrote on 09 dec 2004 in
microsoft.public.inetserver.asp.general:

Did you inspect the rendered HTML with browser view source?
If rsDemande("lien") = true then ...

Which is the same as:

If rsDemande("lien") then ...

=====================

I suppose the OP means by ":" "else" in

Then Response.Write(" checked='checked'") : Response.Write("")

That is not vbscript. [not even quite jscript]

======================

I suggest you change the code to:

<%
If rsDemande("lien") then x = "CHECKED" else x = ""
%>

<input name="checkbox"
type="checkbox" value="checkbox"
<% =x %>>
 
A

Aaron [SQL Server MVP]

I suppose the OP means by ":" "else" in
Then Response.Write(" checked='checked'") : Response.Write("")

That is not vbscript. [not even quite jscript]

I think you're right about what the OP meant, but it is certainly legal
syntax (it just has nothing to do with conditional logic). Try this:

Response.Write "foo" : Response.Write "bar"

It is merely syntax denoting "pretend you went to the next line"... allowing
you to separate commands on a single line, like ; often acts in JScript.

You will probably find this in several of my code snippets at
www.aspfaq.com, e.g.

rs.close: set rs = nothing
conn.close: set conn = nothing
 
E

Evertjan.

Aaron [SQL Server MVP] wrote on 09 dec 2004 in
microsoft.public.inetserver.asp.general:
Then Response.Write(" checked='checked'") : Response.Write("")

That is not vbscript. [not even quite jscript]

I think you're right about what the OP meant, but it is certainly legal
syntax (it just has nothing to do with conditional logic). Try this:

Response.Write "foo" : Response.Write "bar"

Certainly legal, Aaron.

Response.Write "foobar" : Response.Write ""

But useful in the above code?

=====================

The : has it's only realy useful difference:

if a = 3 then a = 4 : a = a + 1

from the new line:

if a = 3 then a = 4
a = a + 1
 
B

Bob Barrows [MVP]

Evertjan. said:
Bob Barrows [MVP] wrote on 09 dec 2004 in
microsoft.public.inetserver.asp.general:
=====================

I suppose the OP means by ":" "else" in

Then Response.Write(" checked='checked'") : Response.Write("")

Yes, I did miss that.
That is not vbscript. [not even quite jscript]

======================

I suggest you change the code to:

<%
If rsDemande("lien") then x = "CHECKED" else x = ""

This would have to be:
If rsDemande("lien") then: x = "CHECKED": else: x = "": end if

The single-line version of if...then does not allow the use of else or
elseif clauses.

Better yet, use multiple lines for easier maintenance.

If rsDemande("lien") then
x = "CHECKED"
else
x = ""
end if

Don't like typing? How about this shortcut:

x=""
If rsDemande("lien") then x = "CHECKED"

Bob Barrows
 
A

Aaron [SQL Server MVP]

I think you're right about what the OP meant, but it is certainly legal
Certainly legal, Aaron.

Response.Write "foobar" : Response.Write ""

But useful in the above code?

Can you please read again what I wrote above. I was merely commenting on
"That is not vbscript." Which is an incorrect statement. Maybe you meant
that is not how to achieve what you think is useful for the OP with
VBScript. Again, please re-read my initial reply.

Thanks.
Aaron
 
E

Evertjan.

Bob Barrows [MVP] wrote on 09 dec 2004 in
microsoft.public.inetserver.asp.general:
This would have to be:
If rsDemande("lien") then: x = "CHECKED": else: x = "": end if

The single-line version of if...then does not allow the use of else or
elseif clauses.

Certainly not, Bob.

according to specs,
<http://tinyurl.com/5gxm3>

1
The single line if can have an "else",
but NEVER should have an "end if", nor an elseif.

2
The single line if is NOT converted to a block/multiline one by ":"s,
because a single line if is defined by having ANY code after the "Then",
other than remarks.

===============

HOWEVER, in practice, vbscript [tested on IE6] does not complain to:

if a then b=0 else b=1 end if

[older basics would not like that!]

but errors out on:

if a then b=0 elseif b=1 else b=2 end if
 
E

Evertjan.

Aaron [SQL Server MVP] wrote on 09 dec 2004 in
microsoft.public.inetserver.asp.general:
Can you please read again what I wrote above. I was merely commenting on
"That is not vbscript." Which is an incorrect statement.

Right, Aaron.

What I ment was:

":" is not an alternative for "else" in vbscript,
like the jscript

r = (a==1) ? 3 : 7

which I surmized [and still do] the OP ment by the ":".
 
B

Bob Barrows [MVP]

Evertjan. said:
Bob Barrows [MVP] wrote on 09 dec 2004 in
microsoft.public.inetserver.asp.general:


Certainly not, Bob.

according to specs,
<http://tinyurl.com/5gxm3>

1
The single line if can have an "else",
but NEVER should have an "end if", nor an elseif.

Oops - my mistake. I knew I should have looked this up first. I thought I
had that down pat, darnit. Teach me to depend on my failing memory ...
2
The single line if is NOT converted to a block/multiline one by ":"s,
because a single line if is defined by having ANY code after the
"Then", other than remarks.

You're right, of course. I would have realized if I had bothered to look it
up before spouting off..
Using the ":"s will work, unless elseif is involved. This raises an error:
x=""
If true then: x = "CHECKED": elseif x = "unchecked" then: x="wrong": end if
msgbox x

Of course, if "elseif" is not involved, there seems to be no need for the
colons, as you point out below.
===============

HOWEVER, in practice, vbscript [tested on IE6]
does not complain to:

if a then b=0 else b=1 end if

It doesn't? It would not have occurred to me to try it. The docs say this
should fail. Interesting. Thanks.
[older basics would not like that!]

True, which explains why I';ve never tried it.
but errors out on:

if a then b=0 elseif b=1 else b=2 end if

as espected. I wonder why the first one succeeds? I think I'll run this by
Eric Lippert.

Bob Barrows
 
B

Bob Barrows [MVP]

Bob said:
Evertjan. said:
HOWEVER, in practice, vbscript [tested on IE6]
does not complain to:

if a then b=0 else b=1 end if

It doesn't? It would not have occurred to me to try it. The docs say
this should fail. Interesting. Thanks.
as espected. I wonder why the first one succeeds? I think I'll run
this by Eric Lippert.

I just received this reply from Eric:
**************************************************
Hey Bob,

There's a story there actually, which I've mentioned in passing on my
blog but I don't think I ever actually told the story.

It's a mistake. The guy who wrote the parser simply forgot to check
whether it was a multi-line or single-line "if" statement when parsing
the "end if". As you correctly note, single-line "if" in VB is an
error, but we accidentally made it legal in VBScript.

We shipped VBScript 1.0 like that just before I started full-time at
Microsoft. For the v2 release, I noticed the bug and checked in a fix
on the 6th of June, 1997. When we shipped the beta, of course every web
page which used the buggy syntax broke. One of the sites which broke
was one of the major computer news sites -- ZDNet perhaps? One of the
big guys, anyway. And they sent us a note saying that they'd give IE4 a
bad review if scripting wasn't 100% backwards compatible.

So on the 13th of August, 1997, I reverted the fix, and it's been that
way ever since. We certainly don't dare change it now -- who knows how
many important ASP pages would stop working? So it'll be like that
forever.

The moral of the story is that you've really got to get it right the
first time. You don't get second chances.
**************************************************

Very interesting, at least to me. I hope nobody else was bored.

Bob Barrows
 
J

Jerome

Ok, it works now!

Thanks a lot everybody :)


Evertjan. said:
Bob Barrows [MVP] wrote on 09 dec 2004 in
microsoft.public.inetserver.asp.general:



Did you inspect the rendered HTML with browser view source?

If rsDemande("lien") = true then ...


Which is the same as:

If rsDemande("lien") then ...

=====================

I suppose the OP means by ":" "else" in

Then Response.Write(" checked='checked'") : Response.Write("")

That is not vbscript. [not even quite jscript]

======================

I suggest you change the code to:

<%
If rsDemande("lien") then x = "CHECKED" else x = ""
%>

<input name="checkbox"
type="checkbox" value="checkbox"
<% =x %>>
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top