August 16, 2002 - Consuming the add Web Service using Code Behind | WebReference

August 16, 2002 - Consuming the add Web Service using Code Behind

Yehuda Shiran August 16, 2002
Consuming the add Web Service using Code Behind
Tips: August 2002

Yehuda Shiran, Ph.D.
Doc JavaScript

The ASP.NET page is much shorter when you use Code Behind. Here is simpleCalcConsumerForm.aspx:

  <%@ Page LANGUAGE="JScript" SRC="simpleCalcConsumerForm.aspx.js" 
    INHERITS="ASPPlus.codeBehind" %>
  <HTML>
  <HEAD>
    <TITLE>simpleCalc XML Web Service Test</TITLE>
  </HEAD>
  <BODY STYLE="font-size:12; font-family:arial,verdana,sans-serif;">
    <FORM RUNAT="server">
      <TABLE BORDER="0">
        <TR><TD>Enter First Number:</TD><TD><ASP:TEXTBOX RUNAT="server" 
           SIZE="4" ID="first" STYLE="text-align:'right'"/></TD></TR>
        <TR><TD>Enter Second Number:</TD><TD><ASP:TEXTBOX RUNAT="server" 
           SIZE="4" ID="second" STYLE="text-align:'right'"/></TD></TR>
        <TR><TD align="right"><ASP:BUTTON TEXT="Add" ID="doAdd"  
           OnClick="Submit_Click" RUNAT="server"/></TD><TD><HR></TD></TR>
        <TR><TD></TD><TD><ASP:TEXTBOX ID="resultControl" RUNAT="server" 
           STYLE="text-align:'right'" SIZE="4"/></TD></TR>
      </TABLE>
    </FORM>
  </BODY>
  </HTML>
The top line is a directive line. It instructs the Web server about the LANGUAGE, SRC, and INHERITS. The SRC specifies the Code Behind file name. The Web server reads the INHERITS attribute and searches the bin directory (underneath the current working directory) for a dll file that includes the definition of ASPPlus.codeBehind. If the Web server does not find this class, it will generate the dll file from the code specified by the SRC attribute.

As we have explained in Column 113, there are three ASP:TextBox controls and one ASP:Button control. The TextBox controls are for entering the two numbers and displaying the result. The Button is for triggering the addition between these two numbers. You define the event handler of this button by the OnClick attribute, OnClick="Submit_Click". The Submit_Click() function is defined in the Code Behind. It is responsible for calling the add Web service with the input numbers, and displaying the result in the bottom ASP:TextBox control. For each TextBox we specified its length (4), and its text-alignment (right).

The communication between the ASP.NET page and the Code Behind page is done via the element IDs. The Code Behind reads the values of the input numbers from their Text property: first.Text and second.Text. The result TextBox is disabled by setting resultControl.Enabled to false. It is set to the result of the addition by setting its Text property, resultControl.Text, to the addition result.

To learn more about JScript .NET and ASP.NET, go to Column 115, JScript .NET, Part IX: Code Behind.