Writing Client Script with Server Script

There's a difference between <script> tags intended for server-side script and <script> tags for client-side script. The ASP.NET framework assumes you want the script to execute on the client unless you specifically mark it as server-side script using the runat="server" attribute. You can include both server- and client-side <script> tags in the same ASP file, but there's a trick. Listing 20.2 shows an example.

Listing 20.2: Combining Server and Client <script> Tags (ch20-2.aspx)

<%@ Page Language="vb" AutoEventWireup="false"

Codebehind="ch2 0-2.aspx.cs" Inherits="CSharpASP.ch2 0.ch2 0_2"%> <html>

<title>ch2 0_2test</title>

<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">

<meta name="CODE_LANGUAGE" content="Visual Basic 7.0"> <meta name="vs defaultClientScript" content="JavaScript"> <meta name="vs targetSchema"

content="http://schemas.microsoft.com/intellisense/ie5"> <script language="C#" runat="server"> void writeShowAlertScript() {

Response.Write( "<scr" + "ipt language=\"JavaScript\">" +

System.Environment.NewLine); Response.Write("function showAlert() {" +

System.Environment.NewLine); Response.Write("alert('Hello from showAlert function!'); "

+ System.Environment.NewLine); Response.Write("}" + System.Environment.NewLine); Response.Write( "alert('Hello world');" +

System.Environment.NewLine); Response.Write( "showAlert();" +

System.Environment.NewLine); Response.Write("</scr" + "ipt>");

<body MS_POSITIONING="GridLayout">

<form id="Form1" method="post" runat="server">

<asp:Label id="Label1" style="Z-INDEX: 101; LEFT: 41px; POSITION: absolute; TOP: 26px" runat="server" Width="439px" Height="58px">The script that shows the alerts on this page was written dynamically with inline server-side code.</asp:Label> <asp:Label id="lblMessage" style="Z-INDEX: 102; LEFT:

41px;

POSITION: absolute; TOP: 147px" runat="server" Width="605px"></asp:Label> </form>

<% writeShowAlertScript();%> </body> </html>

When you run the file, the first thing you see is the two alerts. Which one do you see first? Note that the server script between the code (<% %>) tags at the bottom of the file calls the server-side write-showAlertScript function that generates the client script. Where does the client script appear in the output sent to the browser? Hint: Browse to the file, close the alerts, and then view the browser source code.

The file shows you a trick. The writeShowAlertScript method breaks the client-side <script> tag itself into separate parts. If you don't do this, the ASP engine tries to compile the <script> tag meant for the client and generates an error. To see this in action, change the first and last lines to recombine the word script into a single word:

void writeShowAlertScript() {

Response.Write( "<script language=\"JavaScript\">" +

System.Environment.NewLine); Response.Write("function showAlert() {" +

System.Environment.NewLine); Response.Write("alert('Hello from showAlert function!'); "

+ System.Environment.NewLine); Response.Write("}" + System.Environment.NewLine); Response.Write( "alert('Hello world');" +

System.Environment.NewLine); Response.Write( "showAlert();" +

System.Environment.NewLine); Response.Write("</script>");

Execute the file by requesting it from the browser. You'll see a compilation error stating that the line containing the closing </script> has a newline character in a constant. Of course that's not true—it's a spurious error that occurs when the parser encounters the second <script> tag (see Figure 20.1).

^ n'MMiJVH

Figure 20.1: If you don't "break up" the <script> tag when you write script with inline code, you'll get a spurious error

After each line of the client-side script, the server script writes a carriage return/linefeed character combination by calling the System.Environment.NewLine method. The call to NewLine produces two characters in Windows: the ASCII characters 13 and 10. The NewLine calls are optional with JavaScript, which uses semicolons to delimit the ends of code lines, but they're required for VBScript, which relies on the end-of-line character to delimit the code lines. I usually write them for both languages because, in the browser, the NewLine character makes the code easier to read for debugging purposes.

Note If you've been recompiling between making changes to the HTML (ASPX) files in this chapter, you can stop. The ASP.NET framework automatically detects changes to text file resources such as ASPX files and CONFIG files. Interestingly, when the framework detects such a change, not only does it refresh the cached HTML, but it also parses and JITs any server-side code that appears in the file. Therefore, you can save yourself a little time. You don't have to recompile when you alter HTML files, even those containing server-side scripts; let the framework handle the changes. Of course, you should also recognize that forcing the server to JIT inline code is slower than using precompiled code in a code-behind class, but it's a very useful technique for writing debugging code in the field.

Was this article helpful?

0 0

Post a comment