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

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

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.


People who read this tip also read these tips:

Look for similar tips by subject:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint