Home   Cover Cover Cover Cover
 

aspx Page With Script Tags


From Section 6.1 of the book

Script code can be collected in a method, which has to be embedded in script tags. In the following example the method CounterValue() is executed on the server.

Simple2.aspx
<%@ Page Language="C#" %>
<%@ Import namespace="System.IO" %>
<html>
  <head>
    <title>Page Counter</title>

    <script Language="C#" Runat="server">
      int CounterValue() {
        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();
        return n;
      }
    </script>

  </head>
  <body>
    <h1>Welcome</h1>
    You are visitor number <%= CounterValue() %> to this page!
  </body>
</html>

Try it

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