Udostępnij za pośrednictwem


Displaying from ASP.NET

There are several ways to display information from an ASP.NET program. One approach is to use the <%= %> construction. Another approach is to use the Response.Write statement.

Using <%= %>

The simplest way to display information from an ASP.NET program is to use the <%= %> construct. The value that is entered after the equals sign is written into the current page. The following code displays the value of the variable name.

Hello <%= name %>!

If the value of name were "Frank", the code would write the following string in the current page:

Hello Frank!

The <%= %> construct is most useful for displaying single pieces of information.

The Response.Write Statement

Another way to display text is to use the Response.Write statement. It can be enclosed within a <% %> block.

<% Response.Write("Hello, World!") %>

The Response.Write statement can also be used in a function or method within a script block. The following example shows a function that includes a Response.Write statement.

Note

In ASP.NET pages, functions and variables should be defined within <script> blocks, while executable code must be enclosed within<% %> blocks.

<script runat="server" language="JScript">
   function output(str) {
      Response.Write(str);
   }
   var today = new Date();
</script>
Today's date is <% output(today); %>. <BR>

The output of the Response.Write statement is incorporated into the page being processed. This allows the output of Response.Write to write code that in turn displays text. For example, the following code writes a script block that displays the current date (on the server) in an alert window of the browser accessing the page. The <script> tag is split so the server will not process the tag.

<script runat="server" language="JScript">
   function popup(str) {
      Response.Write("<scr"+"ipt> alert('"+str+"') </scr"+"ipt>");
   }
   var today = new Date();
</script>
<% popup(today); %>

For more information, see Response.

See Also

Concepts

ASP.NET Overview

Other Resources

Displaying Information with JScript