技术开发 频道

ASP.NET AJAX:当我们不需要state


代码5: Person type的定义
using System; 

/// <SUMMARY>
/// Person entity
/// </SUMMARY>public class Person {

private int _personId;
private string _firstName;
private string _lastName;

public Person() { }

public Person(int personId, string firstName, string lastName) {
_personId = personId;
_firstName = firstName;
_lastName = lastName;
}

public int PersonId {
get { return _personId; }
}

public string FirstName {
get { return _firstName; }
set { _firstName = value; }
}

public string LastName {
get { return _lastName; }
set { _lastName = value; }
}

}
    我们对GetPeople方法感兴趣,GetPeople方法返回一堆类型Person(Person类型代码显示在代码6中)。对于下面一个例子将调用GetPeople这个方法,然后重新叙述项目,输出一个HTML无序表(如代码6)。

Collapse 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PeopleList.aspx.cs"
Inherits="NoStateViewPeople" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">

<title>Untitled Page</title>

<script type="text/javascript">
function GetPeople() {
PeopleWebService.GetPeople(OnCompleteCallback, OnServiceTimeout,
OnServiceError);
}

function OnCompleteCallback(result) {
for(i = 0; i < result.length; i++) {
$get("people").innerHTML += "<li>" + result[i].LastName + ", " +
result[i].FirstName + "</li>";
}
}

function OnServiceTimeout(result) {
alert(result);
}

function OnServiceError(result) {
alert(result);
}
</script>

</head>
<body>

<form id="form1" runat="server">

<asp:ScriptManager
ID="sm"
runat="server">
<Services>
<asp:ServiceReference
Path="~/WebServices/PeopleWebService.asmx" />
</Services>
</asp:ScriptManager>

<input
type="button"
value="Get People"
onclick="GetPeople()" />

<ul
id="people"></ul>

</form>
</body>
</html>
0
相关文章