spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column115


JScript .NET, Part IX: Code Behind

Business Systems Analyst - Clearing - SQL Server - ASP - VB (IL)
Next Step Systems
US-IL-Chicago

Justtechjobs.com Post A Job | Post A Resume
Developer News
OpenOffice 3.2 Lands Amid Critical Changes
Red Hat, IBM Firmly in KVM Virtualization Camp
Red Hat Talks Up Open Source Cloud Plans


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


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Search Engine Optimization: Selecting and Embedding Keywords · Are Google's Language Translation Web Services Ready for Prime Time? · Installing and Using Meeplace, the Business Review CMS
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
IBM DB2 10 for z/OS: Justifying the Upgrade · Living La Vida Colo: Choosing the Right Colocation Facility · FTC Concerns over Social Media Privacy Linger


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

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