JScript .NET, Part IX: Code Behind: The Code Behind Hello World - Doc JavaScript | WebReference

JScript .NET, Part IX: Code Behind: The Code Behind Hello World - Doc JavaScript


JScript .NET, Part IX: Code Behind

The Code Behind of Hello World

Let's demonstrate the Code Behind concept with an ASP.NET page that prints the message "Hello World, Code Behind". Here is the JScript .NET file:

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. As we will show later in this column, 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.

Once you have your code written, the next step is to compile it into a dll file in the bin directory underneath the working directory where you keep your code. When the Web server displays an ASP.NET page (.aspx), it searches all dll files in the bin directory for the namespaces that the ASP.NET page requires. When compiling the JScript .NET page (helloworld.aspx.js), you need to instruct the compiler to put the dll file in the bin directory:

jsc /t:library /out:bin\codebehind.dll helloworld.aspx.js

The name of the dll file is not that important, because the Web server searches all dll files for the required namespace. The following Command Prompt window shows the code listing and the compilation stage:


Next: How to write the Hello World's ASP.NET page


Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: July 29, 2002
Revised: July 29, 2002

URL: https://www.webreference.com/js/column115/3.html