single qnd double quotes are not enough

  • Thread starter Thread starter AAaron123
  • Start date Start date
A

AAaron123

<asp:Button onclick="window.location.href = '<%# "Events_view.aspx?Eventid="
&truncate( Eval("id"))%>' "



The above is what I need except the inner most double quotes and the outer
most double quotes conflict.

Can you see a way around this?



thanks
 
<asp:Button onclick="window.location.href = '<%# "Events_view.aspx?Eventid="
&truncate( Eval("id"))%>' "

The above is what I need except the inner most double quotes and the outer
most double quotes conflict.

Can you see a way around this?

thanks

<asp:Button onclick='<%# "window.location.href='Events_view.aspx?
Eventid=" & truncate(Eval("id")) %>'
 
Alexey said:
<asp:Button onclick='<%# "window.location.href='Events_view.aspx?Eventid="
& truncate(Eval("id")) %>'

I think you lost a closing quote:

<asp:Button onclick='<%# "window.location.href='Events_view.aspx?Eventid=" &
truncate(Eval("id")) & "'" %>'

;)

Of course, for clarity I'd use String.Format;

<asp:Button onclick='<%#
String.Format("window.location.href='Events_view.aspx?Eventid={0}'",
Eval("id")) %>'

Pete
 
Looks to me what is allowed is simply that they alternate.

That is, if there is a ' ' inside a " " the " " doesn't look inside the ' '
so it will not see a " if it is there.

With that I was doing OK until I came up against the following:



onclick='<%# "window.location.href='Events_Edit.aspx?Action=Edit&id=" &
truncate(Eval("id")) & "'" %>'



I get the message that the tag is not formed ok.

I'm guessing it is the & in Edit&id but can't seem to find a way to handle
it.

Maybe that is not the problem?



Thanks to both Pete Hurst and Alexey Smirnov for the help
 
I get the message that the tag is not formed ok.

I'm guessing it is the & in Edit&id but can't seem to find a way to handle
it.

The error is nothing to do with the quotes, or the &. It's because an
attribute can be either a databinding expression OR a literal value, you
can't mix both. So if you are databinding, you have to concatenate all the
bits of your string *inside* the databind tags.

For instance, this would also cause an error:

<asp:Button runat="server" Text='Some <%# Eval("Text") %>' />

Whereas this is correct:

<asp:Button runat="server" Text='<%# "Some" & Eval("Text").ToString() %>' />

Note also that literal attributes have double quotes, whereas databound
attributes must have single quotes. Again this is indicative of the fact
that they are completely different things.

Finally you missed runat="server" from your original code, which will also
error.

Hope that helps!

Pete
 
The error is nothing to do with the quotes, or the &. It's because an
attribute can be either a databinding expression OR a literal value, you
can't mix both. So if you are databinding, you have to concatenate all the
bits of your string *inside* the databind tags.

For instance, this would also cause an error:

<asp:Button runat="server" Text='Some <%# Eval("Text") %>' />

Whereas this is correct:

<asp:Button runat="server" Text='<%# "Some" & Eval("Text").ToString()%>' />

Note also that literal attributes have double quotes, whereas databound
attributes must have single quotes. Again this is indicative of the fact
that they are completely different things.

Finally you missed runat="server" from your original code, which will also
error.

Hope that helps!

Pete

I agree with Pete
 
That helps but I still am having a problem.

BTW, is it true that an attribute can be inclosed in either single or double
quotes?
Text="Something"
Text='Something'
NavigateURL="Locations_edit.aspx?Action=New&aa=Old"
NavigateURL='Locations_edit.aspx?Action=New&aa=Old'
are all OK?
Even
NavigateURL="Locations_edit.aspx?Action=New'xx'&aa=Old"
NavigateURL='Locations_edit.aspx?Action=New'xx'&aa=Old'
here Locations_edit.aspx would see the Action string New'xx'


Now to my problem.

I had an Asp:HyperLink that I wanted to change to a button (actually there
are many such HyperLinks)

Even though the button documentation does not show the NavigateUrl attribute
I tried the following and it works OK.

asp:Button class="buttonaction" runat="server" Text="Remove"
NavigateUrl='<%# "Events_Edit.aspx?Action=Remove&id=" & truncate(Eval("id"))
%>' />

I suppose I could just leave it that way, but since NavigateUrl does not
appear to be legal I tried to change that attribute to an onclick event
using window.location.href to move to the new window.

Even with all your help I can't get this to work.

Thanks again




The error is nothing to do with the quotes, or the &. It's because an
attribute can be either a databinding expression OR a literal value, you
can't mix both. So if you are databinding, you have to concatenate all the
bits of your string *inside* the databind tags.

For instance, this would also cause an error:

<asp:Button runat="server" Text='Some <%# Eval("Text") %>' />

Whereas this is correct:

<asp:Button runat="server" Text='<%# "Some" & Eval("Text").ToString() %>'
/>

Note also that literal attributes have double quotes, whereas databound
attributes must have single quotes. Again this is indicative of the fact
that they are completely different things.

Finally you missed runat="server" from your original code, which will also
error.

Hope that helps!

Pete

I agree with Pete
 
Are you sure the string you gave is correct?

Here is a copy/paste from my aspx file:
<asp:button id="Button2" runat="Server" text="Remove" onclick='<%#
"window.location.href='Events_view.aspx?Eventid=" & truncate(Eval("id")) &
"'" %>' />

Along with the error message it generates:

Error 5 The server tag is not well formed. I:\My Documents\Visual Studio
2008\WebSites\Test\Events\Events_List.aspx 39

I just copy/pasted the onclick string from your post to my file to be sure
of no typo.


Thanks much for trying

BTW, I get the same error message with Alexey's string.
 
Are you sure the string you gave is correct?

Here is a copy/paste from my aspx file:
<asp:button id="Button2" runat="Server" text="Remove" onclick='<%#
"window.location.href='Events_view.aspx?Eventid=" & truncate(Eval("id")) &
"'" %>' />

Along with the error message it generates:

Error 5 The server tag is not well formed. I:\My Documents\Visual Studio
2008\WebSites\Test\Events\Events_List.aspx 39

Aaron, simply use Chr function to get an apostrophe

Example:

in C#
onclick='<%# "window.location.href=\u0027" + "Events_view.aspx?
Eventid=" + Eval("id") +"\u0027" %>'

in VB
onclick='<%# "window.location.href=" + Chr(39) + "Events_view.aspx?
Eventid=" + Eval("id") + Chr(39) %>'

Chr(39) in VB is the same as '\u0027' in C#

Or make it from the code behind

onclick="CreateScript('<%# Eval("id")%>')"

protected string CreateScript(string id)
{
return string.Format("javascript:..............('{0}');", id);
}

Hope this helps
 
Are you sure the string you gave is correct?

Here is a copy/paste from my aspx file:
<asp:button id="Button2" runat="Server" text="Remove" onclick='<%#
"window.location.href='Events_view.aspx?Eventid=" & truncate(Eval("id")) &
"'" %>' />

Along with the error message it generates:

Error 5 The server tag is not well formed. I:\My Documents\Visual Studio
2008\WebSites\Test\Events\Events_List.aspx 39

Aaron, simply use Chr function to get an apostrophe

Example:

in C#
onclick='<%# "window.location.href=\u0027" + "Events_view.aspx?
Eventid=" + Eval("id") +"\u0027" %>'

in VB
onclick='<%# "window.location.href=" + Chr(39) + "Events_view.aspx?
Eventid=" + Eval("id") + Chr(39) %>'

Chr(39) in VB is the same as '\u0027' in C#

Or make it from the code behind

onclick="CreateScript('<%# Eval("id")%>')"

protected string CreateScript(string id)
{
return string.Format("javascript:..............('{0}');", id);
}

Hope this helps


Got me closer!

The following I also posted in another post:

I've been trying to do this by myself asking questions as I go but maybe it
would be better to ask for basic directions first.
What I doing is trying to replace a HyperLink with a button. This HyperLink
works:
<asp:HyperLink id="RemoveBtn" runat="server" Text="Remove" NavigateUrl='<%#
"Events_Edit.aspx?Action=Remove&id=" & truncate(Eval("id")) %>' />


This does not open the page (Page_Load never runs)
<asp:button id="Button3" runat="Server" text="Remove" onclick='<%#
"window.location.href=" & Chr(39) & "Events_Edit.aspx?Action=Remove&id=" &
truncate(Eval("id")) & Chr(39) %>' />



Can you point me in a better direction?



Thanks
 
AAaron123 said:
BTW, is it true that an attribute can be inclosed in either single or
double quotes?
Text="Something"
Text='Something'

This is NOT true. If you had read the information I posted already, you
would already have the answer to this. So please go back and read my
messages properly!
Now to my problem.

I had an Asp:HyperLink that I wanted to change to a button (actually
there are many such HyperLinks)

Buttons and links are two different things. Perhaps you could use CSS to
*style* your link to *look* like a button.

Pete
 
Pete Hurst said:
This is NOT true. If you had read the information I posted already, you
would already have the answer to this. So please go back and read my
messages properly!


I know what you wrote about databinding expressions, in fact I cut it and
pasted as a comment in my code.
However, I have inherited code, from someone who knows much more than I do,
that contains, for example:
onclick="window.location.href='<%=Server.UrlEncode("Locations_list.aspx")
%>'"

onclick='window.location.href="Locations_edit.aspx?Action=New"'

So I thought that maybe if databinding were not involved the rules were
different.

Or maybe I just didn't know what to think.

Buttons and links are two different things. Perhaps you could use CSS to
*style* your link to *look* like a button.


I think I have this solved. The string you and Alexey gave me helped much.
The problem I had with CSS is that a HyperLink becomes an anchor and my CSS
anchor attributes were being applied.
I probably could have worked on the CSS code but decided to try to change to
a button.



Thanks
 
In case some other confused developer reads this thread I want to pass on
what I found on http://www.w3.org/TR/html4/intro/sgmltut.html#attributes.
This applies to HTML
Not sure about XHTML

By default, SGML requires that all attribute values be delimited using
either double quotation marks (ASCII decimal 34) or single quotation marks
(ASCII decimal 39). Single quote marks can be included within the attribute
value when the value is delimited by double quote marks, and vice versa.
Authors may also use numeric character references to represent double quotes
(") and single quotes ('). For double quotes authors can also use
the character entity reference &quot;.

Which appears to explain why I've seen:
onclick="window.location.href = 'UserInfo.aspx'"

onclick='window.location.href = "UserInfo.aspx"'
 
AAaron123 said:
However, I have inherited code, from someone who knows much more than I
do, that contains, for example:
onclick="window.location.href='<%=Server.UrlEncode("Locations_list.aspx")
%>'"

onclick='window.location.href="Locations_edit.aspx?Action=New"'

So I thought that maybe if databinding were not involved the rules were
different.

Ok, let me clear up the various usages of quotes. I've read your other post,
and while the reference you linked to is correct, there are other things
going on here.

1. In HTML 4.0 and before, both single and double quotes were valid on
attributes
2. In XHTML, only double quotes are allowed. This is simply because that is
required by the syntax of XML.
3. In ASP.NET server controls, double and single quotes mean very specific
things, as per the databinding rules. (This means that .aspx files are not
strictly valid XML)
4. In Javascript, single and double quotes can be used to denote string. In
your example:
onclick="window.location.href='<%=Server.UrlEncode("Locations_list.aspx")
%>'"

This is a Javascript command:
window.location.href='Locations_list.aspx'

And so the double/single quotes here are down to mixing Javascript in with
attributes. It's better to use double-quotes for all your HTML attributes
(this makes it easy to be XHTML compliant anyway) and then you can use
single-quotes if you need inline Javascript for your element events. I
personally don't like putting Javascript code there anymore, it's much
better to use an external Javascript file and attach events using a
3rd-party library such as jQuery.
I think I have this solved. The string you and Alexey gave me helped much.
The problem I had with CSS is that a HyperLink becomes an anchor and my
CSS anchor attributes were being applied.
I probably could have worked on the CSS code but decided to try to change
to a button.

Hyperlinks ARE anchors. That's how you make hyperlinks in HTML. If you want
certain hyperlnks to look different to others, you give them a CSS class,
and define a CSS style rule to make them appear like buttons. If you don't
do this, you're letting yourself in for an accessibility and standards
nightmare.

HTH

Pete
 
Pete Hurst said:
Ok, let me clear up the various usages of quotes. I've read your other
post, and while the reference you linked to is correct, there are other
things going on here.

1. In HTML 4.0 and before, both single and double quotes were valid on
attributes
2. In XHTML, only double quotes are allowed. This is simply because that
is required by the syntax of XML.
3. In ASP.NET server controls, double and single quotes mean very specific
things, as per the databinding rules. (This means that .aspx files are not
strictly valid XML)
4. In Javascript, single and double quotes can be used to denote string.
In your example:


This is a Javascript command:
window.location.href='Locations_list.aspx'

And so the double/single quotes here are down to mixing Javascript in with
attributes. It's better to use double-quotes for all your HTML attributes
(this makes it easy to be XHTML compliant anyway) and then you can use
single-quotes if you need inline Javascript for your element events. I
personally don't like putting Javascript code there anymore, it's much
better to use an external Javascript file and attach events using a
3rd-party library such as jQuery.


Hyperlinks ARE anchors. That's how you make hyperlinks in HTML. If you
want certain hyperlnks to look different to others, you give them a CSS
class, and define a CSS style rule to make them appear like buttons. If
you don't do this, you're letting yourself in for an accessibility and
standards nightmare.

HTH

Pete


Thanks for putting the quote thing into perspective.
Sometimes a short answer (I'm appreciative of all the time a few of you
devote to helping others) that is completely correct and meaningful to
anyone with experience can be misinterrepeted by someone less experienced
(me).

Thanks again
 
Back
Top