JScript .NET, Part IX: Code Behind: The IsPrime Consumer's Code Behind - Doc JavaScript | WebReference

JScript .NET, Part IX: Code Behind: The IsPrime Consumer's Code Behind - Doc JavaScript


JScript .NET, Part IX: Code Behind

The IsPrime Consumer's Code Behind

Let's take the IsPrime Web service example now. We showed you in Column 114 how to consume this Web service from an ASP.NET page. Let's now split the presentation from the business logic by putting the JScript .NET code in a separate Code Behind file. Here is the code:

import System.Diagnostics;
import System.Xml.Serialization;
import System;
import System.Web.Services.Protocols;
import System.ComponentModel;
import System.Web.Services;
import primeProxy;
package ASPPlus {
  class codeBehind extends System.Web.UI.Page {
    public var resultControl :
      System.Web.UI.WebControls.Label;
    public var first :
      System.Web.UI.WebControls.TextBox;
    public function Submit_Click(sender:Object,
        E:EventArgs) : void {
      var result : String;
      var webService : PrimeNumbers;
      webService = new PrimeNumbers();
      result = webService.IsPrime(int.Parse(first.Text));
      resultControl.Text = (result == 0 ?
      " is not a prime number": "
	    is a prime number") ;
    }
  }
}

We use the same namespace (ASPPlus) and class (codeBehind) as in previous pages, so be sure to re-compile your Code Behind before displaying the ASP.NET page. We define two properties and one function. The property resultControl is the Label where we display the result of the IsPrime check. The property first is the TextBox where we read the input number from.

The function Submit_Click() is triggered by the button on the ASP.NET page and it calls the IsPrime Web service. It also sets the Text property of the resultControl Label.

Again, the communication between the ASP.NET page and the Code Behind is through the dll files in the bin directory. The ASP.NET page looks for the namespace ASPPlus, and for the class codeBehind inside it. Therefore, you need to compile the Code Behind into a dll file:

jsc /t:library /out:bin\codebehind.dll /r:bin\sampleproxy.dll
  IsPrimeConsumerForm.aspx.js

Notice that we make sure the compiler finds the primeProxy namespace in the sampleProxy.dll file, by specifying the /r: switch. The following Command Control window shows the code listing as well as the compiler response:


Next: How to write the IsPrime consumer'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/8.html