August 14, 2002 - Writing Code Behind for the add Web service
August 14, 2002 Writing Code Behind for the add Web service Tips: August 2002
Yehuda Shiran, Ph.D.
|
add
Web service from within the ASP.NET page. We now want to split the ASP.NET page to presentation (.aspx
) and Code Behind (.js
). Here is the Code Behind:
import System.Diagnostics;
import System.Xml.Serialization;
import System;
import System.Web.Services.Protocols;
import System.ComponentModel;
import System.Web.Services;
import calcService;
package ASPPlus {
class codeBehind extends System.Web.UI.Page {
public var resultControl : System.Web.UI.WebControls.TextBox;
public var first : System.Web.UI.WebControls.TextBox;
public var second : System.Web.UI.WebControls.TextBox;
public function Page_Load(sender:Object, E:System.EventArgs) : void {
resultControl.Enabled = false;
}
public function Submit_Click(sender:Object, E:System.EventArgs) : void {
var result : String;
var webService : simpleCalc;
webService = new simpleCalc();
result = webService.add(int.Parse(first.Text), int.Parse(second.Text)).ToString();
resultControl.Text = result;
}
}
}
We first import all kinds of basic classes. Most of them are not used, but they don't interfere either, and provide you with a base of classes which more often than not will be used somewhere. The calcService
namespace is the more important one. This namespace includes the add
Web service definition. Please refer to Column 113 to see how we compiled the add
Web service into a dll
file.
The Code Behind defines the ASPPlus
namespace and the codeBehind
class. Included in the class are three properties and two functions. The three properties are those of the three ASP:TextBox
controls on the ASP.NET page (two input numbers and a result). The functions are Page_Load()
and Submit_Click()
.
Recall from our Hello World
example that the Page_Load()
function is called automatically when the page loads. The only action we do in this function is to disable the resultControl
ASP:TextBo
x. It means that the user cannot enter anything in this text box, as it is used to display the result of the addition of the two input numbers.
The Submit_Click()
function is called when the corresponding button is clicked in the ASP.NET page. We first define the string result
and the object of type simpleCalc
, webService
. We call the add
Web service, and put the result in result
. We finally assign result to the Text
property of resultControl
.
To learn more about JScript .NET and ASP.NET, go to Column 115, JScript .NET, Part IX: Code Behind.