Bug in .NET Framework compiling C# scripts?

R

Richard

Hi All,

The ASPX file below runs fine in ASP .NET WebMatrix 0.6 under WinXP/SP2
with IIS 5.1 and .NET Framework 1.0, 1.1 and 2.0.

But if the last scriptString line is eliminated after appending the
closing ">" to the preceding line, the compiler flags that last line in
the browser (Firefox 5.0) with the message:

Compiler Error Message: CS1010: Newline in constant

The same line is flagged immediately in Visual Web Developer 2005 Expr.
Ed., version 8.0.50727.42, with "Newline in constant". VWD also
complains that the final </script> had no matching start tag.

Is this a "universal bug" :), or are both caused by a bug in the .NET
C# compiler?

An ideas? Is this too trivial to worry about?

TIA,
Richard

========= ASPX start ===========
<%@ Page Language="c#" %>
<script runat="server">

public void Page_Load(Object sender, EventArgs e) {

// Form the script that is to be registered at client side.
String scriptString = "";

scriptString += "<script language=JavaScript> ";
scriptString += "function DoClick()";
scriptString += "{";
scriptString += " myForm.show.value='Msg from
Btn.DoClick()'";
scriptString += "}";
scriptString += "</script";
scriptString += ">";

if( !this.IsClientScriptBlockRegistered("clientScript") )
this.RegisterClientScriptBlock("clientScript", scriptString);
}

</script>
<html>
<head>
<!-- Directive above not orig. -- added by both WebMatrix & WebDev
2005 EE -->
</head>
<body topmargin="20">
<form id="myForm" runat="server">
<input id="show" style="WIDTH: 200px" type="text" />
<input onclick="DoClick()" type="button" value="ClickMe" />
</form>
</body>
</html>
========== ASPX end ===========
 
J

Juan T. Llibre

That is not a .Net Framework bug.
The exact same thing happens in Javascript without the .Net Framework being involved.

scriptString builds a JavaScript client function.

Separating the closing tag prevents the Javascript from being interpreted on the fly:
scriptString += "</script";
scriptString += ">";

If the JavaScript executed when the page is rendered,
instead of when the button's clicked, you'd have worse problems.
 
R

Richard

Hi Juan,

Thank you very much for responding to my question.

I checked further on this. I've discovered a few things.

1. Just to reiterate: When the declartion of of the script separates
"</script" and ">" in Test.aspx:
-- the file displays fine in Web Matrix's Design view;
-- runs fine in Web Matrix's associated Web Server; and ultimately
-- displays a form in Firefox which works fine.

2. However, if I double-click Test.aspx, Visual Web Developer comes
up and barfs immediately. Among other things, it flags the line:
scriptString += "</script";
with the complaint:
Cannot switch views: Validation (Internet Explorer 6): Character
'"' is not valid inside closing tags.

3. In an effort to understand your assessment "The exact same thing
happens in Javascript without the .Net Framework being involved", I
composed the HTML file shown below which runs fine in Firefox and IE
(with the closing script tag written normally.)

If you have time, could you take a further look at my experiences and
assess it further.

Again, thanks for taking the time to consider my confusion.

Yours truly,
Richard

=============== Test.html =============
<html>
<head>
<script language=JavaScript>
function DoClick()
{
myForm.show.value='Msg from Btn.DoClick()'
}
</script>
</head>
<body topmargin="20">
<form id="myForm">
<input id="show" style="WIDTH: 200px" type="text" />
<input onclick="DoClick()" type="button" value="ClickMe" />
</form>
</body>
</html>
=============== Test.html =============
 
J

Juan T. Llibre

You need to understand the difference between writing a Javascript block in a file
which will only be rendered and displayed, and writing a Javascript block in a file,
like in ASP and ASP.NET, which needs to be interpreted, compiled and displayed.





Hi Juan,

Thank you very much for responding to my question.

I checked further on this. I've discovered a few things.

1. Just to reiterate: When the declartion of of the script separates
"</script" and ">" in Test.aspx:
-- the file displays fine in Web Matrix's Design view;
-- runs fine in Web Matrix's associated Web Server; and ultimately
-- displays a form in Firefox which works fine.

2. However, if I double-click Test.aspx, Visual Web Developer comes
up and barfs immediately. Among other things, it flags the line:
scriptString += "</script";
with the complaint:
Cannot switch views: Validation (Internet Explorer 6): Character
'"' is not valid inside closing tags.

3. In an effort to understand your assessment "The exact same thing
happens in Javascript without the .Net Framework being involved", I
composed the HTML file shown below which runs fine in Firefox and IE
(with the closing script tag written normally.)

If you have time, could you take a further look at my experiences and
assess it further.

Again, thanks for taking the time to consider my confusion.

Yours truly,
Richard

=============== Test.html =============
<html>
<head>
<script language=JavaScript>
function DoClick()
{
myForm.show.value='Msg from Btn.DoClick()'
}
</script>
</head>
<body topmargin="20">
<form id="myForm">
<input id="show" style="WIDTH: 200px" type="text" />
<input onclick="DoClick()" type="button" value="ClickMe" />
</form>
</body>
</html>
=============== Test.html =============
 
R

Richard

Hi Juan,
they don't have the closing script tag separated

I misspoke in the above claim. They have the preceding "<" and "/"
splashed over the tail-ends of the immediately preceding lines.

I thought that was aesthetically displeasing, which is why I combined
them, but then I made tthe minimal change I could to get the thing
working: I demoted the closing ">" to the following line.

Regards,
Richard
 
J

Juan T. Llibre

Hi, Richard. Thanks for the correction.
This is a scenario which exists since ASP 2.0.
 
R

Richard

Hi Juan,

I've played around a lot more with this and I'd appreciate knowing
whether the results provoke any additional thoughts.

Also, following up on your original suggestion:
You need to understand the difference between writing a Javascript block in a file
which will only be rendered and displayed, and writing a Javascript block in a file,
like in ASP and ASP.NET, which needs to be interpreted, compiled and displayed.

I wonder if you could point me to a link or two that expounds on that
difference?

I ran three tests on each of three processors, with the following
results, for the C# code presented in
http://msdn.microsoft.com/library/d...UIPageClassRegisterClientScriptBlockTopic.asp:

The tests were:
1. The original code with just Page/Language directive prepended.
2. The original code with directive and comments.
3. The modified code with directive and comments.

The modification consisted mainly of pulling all the delimiters for the
closing script tag into the final line, augmented with a manual attempt
at pretty-printing, resulting in:

String scriptString = "";
scriptString += "<script language=JavaScript> ";
scriptString += "function DoClick()";
scriptString += "{";
scriptString += " myForm.show.value='Msg from
Btn.DoClick()'";
scriptString += "}";
scriptString += "</" + "script" + ">";

All tests ran fine for Web Matrix v. 0.6, with Help/About indicating
".NET Framework Version 1.0.3705.6018"

All tests had "Run" disabled but showed no errors for:
Visual Studio 2002, which reports ".NET Framework Version
1.0.3705"
Visual Web Developer, which reports ".NET Framework Version
2.0.507278"

This seems pretty weird to me. Do these results suggest anything new
to you?

Regards,
Richard
 
J

Juan T. Llibre

re:
I wonder if you could point me to a link or two that expounds on that difference?

Just think about why you need to register client script blocks in ASP.NET.

That very same link you posted :
http://msdn.microsoft.com/library/d...UIPageClassRegisterClientScriptBlockTopic.asp

has lots of information.

The same goes for RegisterStartupScript.
http://msdn.microsoft.com/library/d...lassRegisterStartupScriptTopic.asp?frame=true

For more details, check out Scott Mitchell's article
"Injecting Client-Side Script from an ASP.NET Server Control" :
http://msdn.microsoft.com/library/d...html/aspnet-injectclientsidesc.asp?frame=true
 
R

Richard

Hi Juan,

Thanks for the links: I promise to study them assiduously to solve my
problem.

And thanks also for taking the time to respond to my repeated posts.
At the risk of "overstaying my welcome", can I prevail upon you to
consider the central issue of my earlier post today, namely, why is it
that the example I got from the
http://msdn.microsoft.com/library/d...UIPageClassRegisterClientScriptBlockTopic.asp
site runs on Web Matrix but not on Visual Studio .NET nor on Visual Web
Developer 2005 EE? Not suprisingly, my pretty-printed version had the
same experience.

Again, I promise to study the references closely to see if I can glean
and answer to that question myself.

Kindest regards,
Richard
 
J

Juan T. Llibre

R

Richard

See it working, copied verbatim from the example at that page, at :

Mucho Gracias, er, my Spanish may be a little weak, and no match for
your English :)
Windows Server 2003 SP1 y Microsoft-IIS/6.0 usando ASP.NET 2.0.50727.42

I suspect THAT makes all the difference! I was running on WinXP/SP2
with IIS 5.1.

My server's been down for a while, but I hope to get it restored by
this weekend with the same configuration as yours. I have a strong
feeling everything I've done will work there (except for adding the
closing script tag as 'scriptString += "</script>";'). I'll let you
know.

Again, Jaun, thanks for taking all this time to help me with this
conundrum.

Best wishes,
Richard
 
R

Richard

Hi Juan,

Well, I finally got Windows 2003 Server (Standard Ed.)/SP1 up and
running. I copied over my test versions of the example. All the ones
that failed with a couple of packages on my XP-Pro/SP2 workstation
worked fine when copied to the server and then accessed from my
workstation.

In particular, my favorite worked fine. It ending with:
scriptString += "</" + "script" + ">";

My most favorite failed, just as you suggested it would. It ending
with:
scriptString += "</script>";

So, I still contend that there are bugs in both VWD 2005 EE and VS.NET
or, most likely in IIS 5.1 because it uses .NET Framework 1.1 instead
of 2.0

But I concede that you are right about ending the script with:
scriptString += "<script>";
will fail even in presumably the best environment (IIS 6.0 and .NET
Framework 2.0).

But I'm still hung up on the idea that it should NOT fail. I wrote my
first program (on paper) in about 1955 after a friend in Princeton
graduate math dept. sent me a letter about this new (to us) wonderful
machine: a computer. After college and a year of graduate math at MIT,
I slowly swiched to a professional software developer, which I've been
for the last 40+ years.

So I think I have fair intuition about what ASP.NET does in translating
..aspx to .html, notwithstanding that I'm new to this particular
technology. And I can't imagine why that string has to processed
beyond creating intermediate code and generating the script text in the
html page until the browser processes the script, which should entail
only caching until the browser receives the button's click event.

But I guess I'll have to live with this situation until some future
day.

Best wishes,
Richard
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top