August 17, 2002 - Consuming the IsPrime Web Service using Code Behind
August 17, 2002 Consuming the IsPrime Web Service using Code Behind Tips: August 2002
Yehuda Shiran, Ph.D.
|
IsPrime
Web service from an ASP.NET page. Now, let's 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 other 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:To learn more about JScript .NET and ASP.NET, go to Column 115, JScript .NET, Part IX: Code Behind.