
Passing and Returning Complex Types
If the Web service method returns a complex type, the succeeded callback function receives a return value in the form of a JavaScript object that corresponds to the server type.
The following example shows a Web service method that returns a complex type. The first part of the example shows a Web page that calls a Web service by using client script.
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Script.Serialization" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head id="Head1" runat="server">
<title>Receiving Complex Type</title>
<style type="text/css">
body { font: 11pt Trebuchet MS;
font-color: #000000;
padding-top: 72px;
text-align: center }
.text { font: 8pt Trebuchet MS }
</style>
</head>
<body>
<h2>Receiving Complex Type</h2>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="scriptManager">
<Services>
<asp:ServiceReference Path="HandleColor.asmx" />
</Services>
<Scripts>
<asp:ScriptReference Path="HandleColor.js" />
</Scripts>
</asp:ScriptManager>
<table style="font-size:12px">
<tr>
<td>Web Service Default Color:</td>
<td>
<button id="Button1"
onclick="GetDefaultColor(); return false;">Get Default Color</button>
</td>
</tr>
</table>
<hr />
<!-- Display current color object. -->
<p>
<span style="background-color:Yellow">Color:</span>
<span id="ResultId"></span>
</p>
</form>
</body>
</html>
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head id="Head1" runat="server">
<title>Receiving Complex Type</title>
<style type="text/css">
body { font: 11pt Trebuchet MS;
font-color: #000000;
padding-top: 72px;
text-align: center }
.text { font: 8pt Trebuchet MS }
</style>
</head>
<body>
<h2>Receiving Complex Type</h2>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="scriptManager">
<Services>
<asp:ServiceReference Path="HandleColor.asmx" />
</Services>
<Scripts>
<asp:ScriptReference Path="HandleColor.js" />
</Scripts>
</asp:ScriptManager>
<table style="font-size:12px">
<tr>
<td>Web Service Default Color:</td>
<td>
<button id="Button1"
onclick="GetDefaultColor(); return false;">Get Default Color</button>
</td>
</tr>
</table>
<hr />
<!-- Display current color object. -->
<p>
<span style="background-color:Yellow">Color:</span>
<span id="ResultId"></span>
</p>
</form>
</body>
</html>
The next part of the example shows the client script that is used by the page to call the Web service.
// It gets the default color
// from the Web service.
function GetDefaultColor()
{
// Call the Web service method to get
// the default color.
Samples.AspNet.HandleColor.GetDefaultColor(
SucceededCallback);
}
// This is the callback function that
// processes the complex type returned
// by the Web service.
function SucceededCallback(result)
{
// Read the values returned by the
// Web service.
var message = result.message;
var rgb = result.rgb;
var timeStamp = result.timeStamp;
// Transform the rgb array into a string.
var serverColor = rgb[0]+ rgb[1] + rgb[2];
// Display the result.
var displayResult =
document.getElementById("ResultId");
displayResult.style.color = "yellow";
displayResult.style.fontWeight = "bold";
if (document.all)
displayResult.innerText = message + " " + timeStamp;
else
// Firefox
displayResult.textContent = message + " " + timeStamp;
displayResult.style.backgroundColor = "#" + serverColor;
}
if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
The next part of the example shows the Web service that is called by the page.
<%@ WebService Language="VB" Class="Samples.AspNet.HandleColor" %>
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Web.Script.Services
Imports System.Web.Script.Serialization
Namespace Samples.AspNet
' Define the color object to
' exchange with the client.
Public Class ColorObject
Public message As String = "The default color is Blue."
Public rgb() As String = {"00", "00", "FF"}
Public timeStamp As String
End Class 'ColorObject
<WebService([Namespace]:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<GenerateScriptType(GetType(ColorObject))> _
<ScriptService()> _
Public Class HandleColor
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function GetDefaultColor() As ColorObject
' Instantiate the default color object.
Dim co As New ColorObject()
' Set time stamp.
co.timeStamp = DateTime.Now.ToString()
Return co
End Function 'GetDefaultColor
<WebMethod()> _
Public Function ChangeDefaultColor( _
ByVal color As ColorObject) As ColorObject
' Instantiate the default color object.
Dim co As New ColorObject()
' Assign the passed values.
co.message = color.message
co.rgb = color.rgb
' Set time stamp.
co.timeStamp = DateTime.Now.ToString()
Return co
End Function 'ChangeDefaultColor
End Class 'HandleColor
End Namespace
<%@ WebService Language="C#" Class="Samples.AspNet.HandleColor" %>
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
namespace Samples.AspNet
{
// Define the color object to
// exchange with the client.
public class ColorObject
{
public string message =
"The default color is Blue.";
public string[] rgb =
new string[] { "00", "00", "FF" };
public string timeStamp;
}
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[GenerateScriptType(typeof(ColorObject))]
[ScriptService]
public class HandleColor :
System.Web.Services.WebService
{
[WebMethod]
public ColorObject GetDefaultColor()
{
// Instantiate the default color object.
ColorObject co = new ColorObject();
// Set time stamp.
co.timeStamp = DateTime.Now.ToString();
return co;
}
[WebMethod]
public ColorObject ChangeDefaultColor(ColorObject color)
{
// Instantiate the default color object.
ColorObject co = new ColorObject();
// Assign the passed values.
co.message = color.message;
co.rgb = color.rgb;
// Set time stamp.
co.timeStamp = DateTime.Now.ToString();
return co;
}
}
}
The following example shows how to call Web service methods that have parameters that correspond to complex types. Proxy classes for the types will be automatically generated. This enables client script to create instances of the type to pass as parameters to the method call.
The first part of the example shows a Web page that calls a Web service by using client script.
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Script.Serialization" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head id="Head1" runat="server">
<title>Exchanging Complex Types</title>
<style type="text/css">
body { font: 11pt Trebuchet MS;
font-color: #000000;
padding-top: 72px;
text-align: center }
.text { font: 8pt Trebuchet MS }
</style>
</head>
<body>
<h2>Exchanging Complex Types</h2>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="scriptManager">
<Services>
<asp:ServiceReference Path="HandleColor.asmx" />
</Services>
<Scripts>
<asp:ScriptReference Path="HandleColor.js" />
</Scripts>
</asp:ScriptManager>
</form>
<center>
<table style="font-size:12px;" >
<tr align="center">
<td class="text">Change Color:</td>
<td>
<select id="ColorSelectID"
onchange="OnChangeDefaultColor(this);" runat="server">
</select>
</td>
</tr>
</table>
</center>
<hr />
<!-- Display current color object. -->
<span style="background-color:Yellow">Color:</span>
<span id="ResultId"></span>
</body>
</html>
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Script.Serialization" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head id="Head1" runat="server">
<title>Exchanging Complex Types</title>
<style type="text/css">
body { font: 11pt Trebuchet MS;
font-color: #000000;
padding-top: 72px;
text-align: center }
.text { font: 8pt Trebuchet MS }
</style>
</head>
<body>
<h2>Exchanging Complex Types</h2>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="scriptManager">
<Services>
<asp:ServiceReference Path="HandleColor.asmx" />
</Services>
<Scripts>
<asp:ScriptReference Path="HandleColor.js" />
</Scripts>
</asp:ScriptManager>
</form>
<center>
<table style="font-size:12px;" >
<tr align="center">
<td class="text">Change Color:</td>
<td>
<select id="ColorSelectID"
onchange="OnChangeDefaultColor(this);" runat="server">
</select>
</td>
</tr>
</table>
</center>
<hr />
<!-- Display current color object. -->
<span style="background-color:Yellow">Color:</span>
<span id="ResultId"></span>
</body>
</html>
The next part of the example shows the client script that is used by the Web page to call the Web service.
// The Web service default color.
var defaultRgb;
// The page feedback display element.
var displayResult;
// Gets the selection list colors and
// the default color from the Web service.
function GetServerColors()
{
// Gets the default color.
Samples.AspNet.HandleColor.GetDefaultColor(
SucceededCallback, FailedCallback);
// Get selection list colors.
Samples.AspNet.HandleColor.GetColorList(
SucceededCallback, FailedCallback);
}
// This function passes the color selected
// by the user (client) to the Web service.
function OnChangeDefaultColor(comboObject)
{
// Create an instance
// of the server color object.
var color =
new Samples.AspNet.ColorObject();
// Get the user's selected color.
var selectionIndex =
comboObject.selectedIndex;
var selectedColor =
comboObject.options[selectionIndex].text;
// Get the related RGB color value.
var selectionValue =
comboObject.value;
// Transform it into an array.
var colorArray = selectionValue.split(",");
// Assign the new values to
// the server color object.
color.message = "The new default color is " + selectedColor + ".";
color.rgb = colorArray;
// Call the Web service method to change the color.
Samples.AspNet.HandleColor.ChangeDefaultColor(
color, SucceededCallback, FailedCallback);
}
// This is the callback function that processes
// the complex type returned by the Web service.
function SucceededCallback(result, userContext, methodName)
{
switch(methodName)
{
case ("GetColorList"):
{
// Get the select object.
var selectObject = document.getElementById("ColorSelectID");
var i = 0;
// Iterate through the dictionary to populate
// the selection list.
for(var item in result)
{
var option = new Option(result[item], item);
selectObject.options[i]=option;
// Set the default selection.
if (item == defaultRgb)
selectObject.options[i].selected = true;
i++;
}
break;
}
default:
{
// Get the server default color and its current time.
// Read the values returned by the
// Web service.
var message = result.message;
defaultRgb = result.rgb;
var timeStamp = result.timeStamp;
// Transform the rgb array into a string.
var serverColor = defaultRgb[0]+ defaultRgb[1] + defaultRgb[2];
// Display the result.
displayResult.style.color = "yellow";
displayResult.style.fontWeight = "bold";
if (document.all)
displayResult.innerText = message + " " + timeStamp;
else
// Firefox
displayResult.textContent = message + " " + timeStamp;
displayResult.style.backgroundColor = "#" + serverColor;
break;
}
}
}
// Callback function invoked on failure
// of the Web service methods.
function FailedCallback(error, userContext, methodName)
{
if(error !== null)
{
displayResult.innerHTML = "An error occurred: " +
error.get_message();
}
}
// Gets the Web service selection list colors
// and the default color.
function pageLoad()
{
// Get page feedback display element.
displayResult =
document.getElementById("ResultId");
// Get the server's selection list colors and
// the default color.
GetServerColors();
}
if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
The next part of the example shows the Web service that is called by the page.
<%@ WebService Language="C#" Class="Samples.AspNet.HandleColor" %>
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using System.Collections.Generic;
namespace Samples.AspNet
{
// Define the color object to
// exchange with the client.
public class ColorObject
{
public string message;
public string[] rgb;
public string timeStamp;
public ColorObject()
{
this.message = "The default color is Red.";
this.rgb = new string[] { "FF", "00", "00" };
this.timeStamp = DateTime.Now.ToString();
}
}
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[GenerateScriptType(typeof(ColorObject))]
[ScriptService]
public class HandleColor :
System.Web.Services.WebService
{
[WebMethod]
public ColorObject GetDefaultColor()
{
// Instantiate the default color object.
ColorObject co = new ColorObject();
return co;
}
[WebMethod]
public ColorObject ChangeDefaultColor(ColorObject color)
{
// Instantiate the default color object.
ColorObject co = new ColorObject();
// Assign the passed values.
co.message = color.message;
co.rgb = color.rgb;
// Set time stamp.
co.timeStamp = DateTime.Now.ToString();
return co;
}
[WebMethod]
public Dictionary<String,String> GetColorList()
{
//Instatiate the dictionary object.
Dictionary<String, String> result = new Dictionary<string, string>();
//Add the color items.
result.Add("00,00,FF", "Blue");
result.Add("FF,00,00", "Red");
result.Add("00,FF,00", "Green");
result.Add("00,00,00", "Black");
return result;
}
}
}
Passing Parameters Typed as Generics or Arrays
A Web service method might support parameters or a return value that are typed as generics or as arrays of lists of type T. In that case, ASP.NET automatically generates proxy classes for the type T for use with client script.
However, if a generic type takes more than one type argument, such as Dictionary<string, <T>>, ASP.NET does not automatically generate proxy classes for the types. For ASP.NET to generate a proxy class for the type T, the Web service class that uses the type must be qualified with the GenerateScriptTypeAttribute attribute for the type T.
The following example shows how to call a Web service method from client script when the parameters are typed as generics or arrays. The first part of the example shows a Web page that calls a Web service by using client script.
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head id="Head1" runat="server">
<title>Using Generics Proxy Types</title>
<style type="text/css">
body { font: 11pt Trebuchet MS;
font-color: #000000;
padding-top: 72px;
text-align: center }
.text { font: 10pt Trebuchet MS; text-align: center }
</style>
</head>
<body>
<h2>Using Generics Proxy Types</h2>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="scriptManager">
<Services>
<asp:ServiceReference Path="WebService.asmx" />
<asp:ServiceReference Path="WebService2.asmx" />
</Services>
<Scripts>
<asp:ScriptReference Path="generics.js" />
</Scripts>
</asp:ScriptManager>
</form>
<center>
<table style="font-size:12px;" >
<tr align="left">
<td class="text">Generic List:</td>
<td>
<button id="Button1"
onclick="GenericList()">Get List</button>
</td>
</tr>
<tr align="left">
<td class="text">Generic Dictionary:</td>
<td>
<button id="Button2"
onclick="GenericDictionary()">Get Dictionary</button>
</td>
</tr>
<tr align="left">
<td class="text">Generic Custom Type Dictionary:</td>
<td>
<button id="Button3"
onclick="GenericCustomTypeDictionary()">Get Dictionary</button>
</td>
</tr>
<tr align="left">
<td class="text">Generic Dictionary:</td>
<td>
<button id="Button5"
onclick="PassGenericDictionary()">Pass Dictionary</button>
</td>
</tr>
<tr align="left">
<td class="text">Array:</td>
<td>
<button id="Button4"
onclick="ArrayType()">Get Array</button>
</td>
</tr>
</table>
</center>
<hr />
<!-- Display current color object. -->
<span id="ResultId"></span>
</body>
</html>
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head id="Head1" runat="server">
<title>Using Generics Proxy Types</title>
<style type="text/css">
body { font: 11pt Trebuchet MS;
font-color: #000000;
padding-top: 72px;
text-align: center }
.text { font: 10pt Trebuchet MS; text-align: center }
</style>
</head>
<body>
<h2>Using Generics Proxy Types</h2>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="scriptManager">
<Services>
<asp:ServiceReference Path="WebService.asmx" />
<asp:ServiceReference Path="WebService2.asmx" />
</Services>
<Scripts>
<asp:ScriptReference Path="generics.js" />
</Scripts>
</asp:ScriptManager>
</form>
<center>
<table style="font-size:12px;" >
<tr align="left">
<td class="text">Generic List:</td>
<td>
<button id="Button1"
onclick="GenericList()">Get List</button>
</td>
</tr>
<tr align="left">
<td class="text">Generic Dictionary:</td>
<td>
<button id="Button2"
onclick="GenericDictionary()">Get Dictionary</button>
</td>
</tr>
<tr align="left">
<td class="text">Generic Custom Type Dictionary:</td>
<td>
<button id="Button3"
onclick="GenericCustomTypeDictionary()">Get Dictionary</button>
</td>
</tr>
<tr align="left">
<td class="text">Generic Dictionary:</td>
<td>
<button id="Button5"
onclick="PassGenericDictionary()">Pass Dictionary</button>
</td>
</tr>
<tr align="left">
<td class="text">Array:</td>
<td>
<button id="Button4"
onclick="ArrayType()">Get Array</button>
</td>
</tr>
</table>
</center>
<hr />
<!-- Display current color object. -->
<span id="ResultId"></span>
</body>
</html>
The next part of the example shows the client script that is used by the Web page to call the Web service.
// The page feedback display element.
var displayResult;
// This function intializes the global variables and
// assigns default values to the generated proxy.
function pageLoad()
{
// Get page feedback display element.
displayResult =
document.getElementById("ResultId");
// Assign default values to the generated proxy.
Samples.AspNet.TestService.set_defaultUserContext("Default context");
Samples.AspNet.TestService.set_defaultSucceededCallback(SucceededCallback);
Samples.AspNet.TestService.set_defaultFailedCallback(FailedCallback);
}
// Get a generic List.
function GenericList()
{
Samples.AspNet.TestService.GetGenericList();
}
// Get a generic Dictionary.
function GenericDictionary()
{
Samples.AspNet.TestService.GetGenericDictionary();
}
// Get a generic Dictionary of custom types.
function GenericCustomTypeDictionary()
{
Samples.AspNet.TestService.GetGenericCustomTypeDictionary();
}
// Pass a generic dictionary of custom types
// to Webservice.
function PassGenericDictionary()
{
var simple = new Samples.AspNet.SimpleClass2();
simple.s = "WebService proxy.";
Samples.AspNet.TestService.PassGenericDictionary(
{"first":simple});
}
// Get an Array.
function ArrayType()
{
Samples.AspNet.TestService.GetArray();
}
// Callback function invoked when the call to
// the Web service methods succeeds.
function SucceededCallback(result, userContext, methodName)
{
var message;
switch(methodName)
{
case ("GetGenericList"):
{
var i = 0;
var message = new Array();
for(var item in result)
{
message[i] = "List element " + i + ": " + result[item].s;
i++;
}
DisplayMessage(message.toString());
break;
}
case ("GetGenericDictionary"):
{
var i = 0;
var message = new Array();
for(var item in result)
{
message[i] = item + ": " + result[item];
i++;
}
DisplayMessage(message.toString());
break;
}
case ("GetGenericCustomTypeDictionary"):
{
var i = 0;
var message = new Array();
for(var item in result)
{
message[i] = item + ": " + result[item].s;
i++;
}
DisplayMessage(message.toString());
break;
}
case ("PassGenericDictionary"):
{
DisplayMessage(result);
break;
}
case ("GetArray"):
{
var i = 0;
var message = new Array();
for(var item in result)
{
message[i] = result[item];
i++;
}
DisplayMessage(message.toString());
break;
}
default:
{
DisplayMessage("Method unknown");
}
}
}
// Callback function invoked when the call to
// the Web service methods fails.
function FailedCallback(error, userContext, methodName)
{
if(error !== null)
{
displayResult.innerHTML = "An error occurred: " +
error.get_message();
}
}
function DisplayMessage(message)
{
if (document.all)
displayResult.innerText = message;
else
// Firefox
displayResult.textContent = message;
}
if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
The next part of the example shows the Web service that is called by the page.
<%@ WebService Language="C#" Class="Samples.AspNet.TestService" %>
namespace Samples.AspNet
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.Script.Services;
// Custom type used by WebService.
public class SimpleClass
{
private string _s = "SimpleClass1";
public String s {
get { return _s; }
set { _s = value; }
}
}
// Custom type used by WebService.
public class SimpleClass2
{
private string _s = "SimpleClass2";
public String s
{
get { return _s; }
set { _s = value; }
}
}
// The following service allows the generation
// of the SimpleClass2 type proxy by applying the
// attribute: GenerateScriptType. This assures
// that the a SimpleClass2 object can be
// passed by the client script to the
// PassGenericDictionary method.
// Note if you comment out the GenerateScriptType
// you get an error because the SimpleClass2 type proxy
// is not generated.
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[GenerateScriptType(typeof(SimpleClass2))]
[ScriptService]
public class TestService : System.Web.Services.WebService
{
// The following method return a list of SimpleClass
// objects.
// Note the SimpleClass type proxy is automatically
// generated because used in the return parameter.
[WebMethod]
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public List<SimpleClass> GetGenericList()
{
List<SimpleClass> GetGenericList = new List<SimpleClass>();
SimpleClass simple1 = new SimpleClass();
SimpleClass simple2 = new SimpleClass();
simple1.s = "Generics first instance";
simple2.s = "Generics second instance";
GetGenericList.Add(simple1);
GetGenericList.Add(simple2);
return GetGenericList;
}
// The following method return a Dictionary of strings.
[WebMethod]
public Dictionary<String, String> GetGenericDictionary()
{
//Instantiate the dictionary object.
Dictionary<String, String> result =
new Dictionary<string, string>();
//Add items.
result.Add("0000FF", "Blue");
result.Add("FF0000", "Red");
result.Add("00FF00", "Green");
result.Add("000000", "Black");
return result;
}
// The following method return a Dictionary of
// SimpleClass objects.
// Note the SimpleClass type proxy object is automatically
// generated because used in the return parameter.
[WebMethod]
public Dictionary<String, SimpleClass> GetGenericCustomTypeDictionary()
{
//Instantiate the dictionary object.
Dictionary<String, SimpleClass> result =
new Dictionary<string, SimpleClass>();
SimpleClass simple = new SimpleClass();
simple.s = "custom type instance";
//Add items.
result.Add("Custom type", simple);
return result;
}
// The following method accept a Dictionary of
// SimpleClass2 objects.
// Note the SimpleClass2 type proxy object is
// not automatically generated because used in
// an input parameter. You must enable its
// generation by applying the GenerateScriptType
// attribute to the service.
[WebMethod]
public String PassGenericDictionary(
Dictionary<string, SimpleClass2> d)
{
string value = d["first"].s;
string message = "Dictionary element value: " + value;
return message;
}
// This function returns an array.
[WebMethod]
public ArrayList GetArray()
{
//Instantiate the array object.
ArrayList result = new ArrayList();
//Add items.
result.Add("First element: " + "Test1");
result.Add("Second element: " + "Test2");
return result;
}
}
}
Passing Parameters Typed as Enumerators
An enumerator type can be accessed by using the automatically generated proxy class.
Note: |
|---|
Do not instantiate the generated proxy class in order to access the enumerators.
|
The following example shows how to call a Web service method from client script when the parameters are typed as enumerators. The first part of the example shows a Web page that calls a Web service by using client script.
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head id="Head1" runat="server">
<title>Using Generated Proxy Types</title>
<style type="text/css">
body { font: 11pt Trebuchet MS;
font-color: #000000;
padding-top: 72px;
text-align: center }
.text { font: 10pt Trebuchet MS; text-align: center }
</style>
</head>
<body>
<h2>Using Generated Proxy Types</h2>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="scriptManager">
<Services>
<asp:ServiceReference Path="ServerTypes.asmx" />
</Services>
<Scripts>
<asp:ScriptReference Path="ServerTypes.js" />
</Scripts>
</asp:ScriptManager>
</form>
<center>
<table style="font-size:12px;" >
<tr align="left">
<td class="text">Get Enum:</td>
<td>
<button id="Button2"
onclick="GetSelectedEnumValue()">Get Enum Value</button>
</td>
</tr>
<tr align="left">
<td class="text">Pass Enum:</td>
<td>
<button id="Button1"
onclick="GetFirstEnumElement()">First Enum</button>
</td>
</tr>
</table>
</center>
<hr />
<!-- Display current color object. -->
<span id="ResultId"></span>
</body>
</html>
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head id="Head1" runat="server">
<title>Using Generated Proxy Types</title>
<style type="text/css">
body { font: 11pt Trebuchet MS;
font-color: #000000;
padding-top: 72px;
text-align: center }
.text { font: 10pt Trebuchet MS; text-align: center }
</style>
</head>
<body>
<h2>Using Generated Proxy Types</h2>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="scriptManager">
<Services>
<asp:ServiceReference Path="ServerTypes.asmx" />
</Services>
<Scripts>
<asp:ScriptReference Path="ServerTypes.js" />
</Scripts>
</asp:ScriptManager>
</form>
<center>
<table style="font-size:12px;" >
<tr align="left">
<td class="text">Get Enum:</td>
<td>
<button id="Button2"
onclick="GetSelectedEnumValue()">Get Enum Value</button>
</td>
</tr>
<tr align="left">
<td class="text">Pass Enum:</td>
<td>
<button id="Button1"
onclick="GetFirstEnumElement()">First Enum</button>
</td>
</tr>
</table>
</center>
<hr />
<!-- Display current color object. -->
<span id="ResultId"></span>
</body>
</html>
The next part of the example shows the client script that is used by the Web page to call the Web service.
// ServerTypes.js
// The Web service default color.
var defaultRgb;
// The page feedback display element.
var displayResult;
// Gets the Web service selection list colors
// and the default color.
function pageLoad()
{
// Get page feedback display element.
displayResult =
document.getElementById("ResultId");
// Assign default values for the generated proxy using
// the (generated) static proxy.
Samples.AspNet.ServerTypes.set_timeout(200);
Samples.AspNet.ServerTypes.set_defaultUserContext("Default context");
Samples.AspNet.ServerTypes.set_defaultSucceededCallback(SucceededCallback);
Samples.AspNet.ServerTypes.set_defaultFailedCallback(FailedCallback);
}
// This function shows how to get an
// enumeration object from the server.
function GetFirstEnumElement()
{
// Get the first element of the enumeration
Samples.AspNet.ServerTypes.GetFirstColor();
}
// This function shows how to pass an
// enumeration value to the server.
function GetSelectedEnumValue()
{
// Get the value of the selected enumerated
// element.
Samples.AspNet.ServerTypes.GetSelectedColor(
Samples.AspNet.ColorEnum.Blue);
}
// Callback function invoked when the call to
// the Web service methods succeeds.
function SucceededCallback(result, userContext, methodName)
{
var message;
switch(methodName)
{
case ("GetFirstColor"):
{
var firstColor = result;
message = "First enumerated value: " + firstColor;
DisplayMessage(message);
break;
}
default:
{
DisplayMessage("Method unknown");
}
}
}
// Callback function invoked when the call to
// the Web service methods fails.
function FailedCallback(error, userContext, methodName)
{
if(error !== null)
{
displayResult.innerHTML = "An error occurred: " +
error.get_message();
}
}
function DisplayMessage(message)
{
if (document.all)
displayResult.innerText = message;
else
// Firefox
displayResult.textContent = message;
}
if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
The next part of the example shows the Web service that is called by the page.
<%@ WebService Language="VB" Class="Samples.AspNet.ServerTypes" %>
Imports System
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Web.Script.Services
Imports System.Web.Script.Serialization
Imports System.Collections.Generic
Namespace Samples.AspNet
' Define the enum type to
' exchange with the client.
Public Enum ColorEnum
Red = 0
Green = 1
Blue = 2
End Enum
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ScriptService()> _
Public Class ServerTypes
Inherits System.Web.Services.WebService
' The enum type can be accessed by the client
' script through the generated Web service
' proxy class in a static fashion.
<WebMethod()> _
Public Function GetFirstColor() As ColorEnum
' Return the first color
' in the enumeration.
Return ColorEnum.Red
End Function
<WebMethod()> _
Public Function GetSelectedColor(ByVal color As ColorEnum) As String
' Return the selected color value
' in the enumeration.
Return color.ToString()
End Function
End Class
End Namespace
<%@ WebService Language="C#" Class="Samples.AspNet.ServerTypes" %>
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using System.Collections.Generic;
namespace Samples.AspNet
{
// Define the enum type to
// exchange with the client.
public enum ColorEnum
{
Red = 0,
Green = 1,
Blue = 2
}
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class ServerTypes :
System.Web.Services.WebService
{
// The enum type can be accessed by the client
// script through the generated Web service
// proxy class in a static fashion.
[WebMethod]
public ColorEnum GetFirstColor()
{
// Return the first color
// in the enumeration.
return ColorEnum.Red;
}
[WebMethod]
public string GetSelectedColor(ColorEnum color)
{
// Return the selected color value
// in the enumeration.
return color.ToString();
}
}
}