Home   Cover Cover Cover Cover
 

aspx Page With Embedded Script Code


From Section 6.1 of the book

This example shows the simplest form of a dynamic aspx page. The script code is simply embedded in HTML between <% ... %> brackets. The example implements a page counter. The counter value is stored in a file.

Simple1.aspx
<%@ Page Language="C#" %>
<%@ Import namespace="System.IO" %>
<html>
  <head>
    <title>Dynamic aspx page</title>
  </head>
  <body>
    <h1>Welcome</h1>
    You are visitor number <%
      FileStream s = new FileStream(Server.MapPath("Counter.dat"), FileMode.OpenOrCreate);
      int n = 0;
      try {
        BinaryReader r = new BinaryReader(s);
        n = r.ReadInt32();
      } catch {}
      n++;
      s.Seek(0, SeekOrigin.Begin);
      BinaryWriter w = new BinaryWriter(s);
      w.Write(n);
      s.Close();
      Response.Write(n);
    %> to this page!
  </body>
</html>

Try it

   http://dotnet.jku.at/book/samples/6/Simple1.aspx