August 10, 2002 - Hello World's Code Behind Version | WebReference

August 10, 2002 - Hello World's Code Behind Version

Yehuda Shiran August 10, 2002
Hello World's Code Behind Version
Tips: August 2002

Yehuda Shiran, Ph.D.
Doc JavaScript

Let's demonstrate the Code Behind concept with JScript .NET code that prints the message "Hello World, Code Behind":

  package ASPPlus {
   
    class codeBehind extends System.Web.UI.Page {
   
      public var message : System.Web.UI.WebControls.Label;
	
      public function Page_Load(sender:Object, E:System.EventArgs) : void {
        message.Text = "Hello World, Code Behind";
      }
    }
  }
First, notice that we don't import any external namespaces. The reason for this is that we don't reference external classes in this particular example. In general, you would put all your import statements before the package definition. As a reminder, all namespaces are available for the JScript compiler, even without importing them. The only reason to import a namespace is to avoid writing the namespace name in front of the class name, like ASPPlus.CodeBehind.

The package statement defines a namespace, ASPPlus in this case. We define one class in this namespace, codeBehind. This class is an extension of the System.Web.UI.Page base class. Our class includes one property and one function. We use the message property to set the message content of "Hello World, Code Behind". Actually, message is the ID attribute of the ASP:Label control in the ASP.NET page. This is how we link between the ASP.NET page and the JScript .NET code. Notice that message is of type System.Web.UI.WebControls.Label:

  public var message : System.Web.UI.WebControls.Label;
The function Page_Load() is an intrinsic function. It is invoked automatically when the ASP.NET page loads. You don't need to set the onload event handler definition, as you would do in client-side JavaScript. The Page_Load() function includes one statement in which we set the Text property of the message ASP:Label control:

  public function Page_Load(sender:Object, E:System.EventArgs) : void {
    message.Text = "Hello World, Code Behind";
  }
Notice that the function returns void, because we don't return any value from the function, just setting message.Text. Also, the JScript .NET variable and property names are case sensitive. If you would try setting the value of message.text (text begins with a lower case t), you would get a compilation error.

To learn more about JScript .NET and ASP.NET, go to Column 115, JScript .NET, Part IX: Code Behind.