JScript .NET, Part IX: Code Behind: The Code Behind add Consumer - Doc JavaScript
JScript .NET, Part IX: Code Behind
The Code Behind add Consumer
Let's look at a more complex example now. In Column 113 we called the 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 provides 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:TextBox
. 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
.
Next: How to compile the add consumer's Code Behind
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/5.html