PageMethod是Asp的一种更简单、更快的方法.Net AJAX
PageMethod是一种在Java脚本中公开服务器端页面方法的方法.这带来了如此多的机会,我们可以执行大量操作,而无需使用缓慢且烦人的回发.
在这篇文章中,我将展示ScriptManager和PageMethod的基本用法.在本例中,我创建了一个用户注册表,其中用户可以根据他的邮箱地址和密码进行注册.下面是我将要开发的页面的标记:
<body>
<form id="form1" runat="server">
<div>
<fieldset style="width: 200px;">
<asp:Label ID="lblEmailAddress" runat="server" Text="Email Address"></asp:Label>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<asp:Label ID="lblPassword" runat="server" Text="Password"></asp:Label>
<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
</fieldset>
<div>
</div>
<asp:Button ID="btnCreateAccount" runat="server" Text="Signup" />
</div>
</form>
</body>
</html>
要设置page方法,首先必须在页面上拖动脚本管理器.
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
还请注意,我已经更改了EnablePageMethods="true"
.
这将告诉ScriptManager我将从客户端调用PageMethods.
现在,下一步是创建服务器端函数.
下面是我创建的函数,该函数验证用户的输入:
[WebMethod]
public static string RegisterUser(string email, string password)
{
string result = "Congratulations!!! your account has been created.";
if (email.Length == 0)//Zero length check
{
result = "Email Address cannot be blank";
}
else if (!email.Contains(".") || !email.Contains("@")) //some other basic checks
{
result = "Not a valid email address";
}
else if (!email.Contains(".") || !email.Contains("@")) //some other basic checks
{
result = "Not a valid email address";
}
else if (password.Length == 0)
{
result = "Password cannot be blank";
}
else if (password.Length < 5)
{
result = "Password cannot be less than 5 chars";
}
return result;
}
要告诉脚本管理器此方法可以通过javascript访问,我们需要确保两件事:
首先:此方法应该是‘公共静电’.
第二:上面的代码中所写的方法上面应该有一个[WebMethod]标记.
现在我已经创建了服务器端函数来创建帐户.现在我们必须从客户端调用它.下面是我们如何从客户端调用该函数:
<script type="text/javascript">
function Signup() {
var email = document.getElementById('<%=txtEmail.ClientID %>').value;
var password = document.getElementById('<%=txtPassword.ClientID %>').value;
PageMethods.RegisterUser(email, password, onSucess, onError);
function onSucess(result) {
alert(result);
}
function onError(result) {
alert('Cannot process your request at the moment, please try later.');
}
}
</script>
要调用我的服务器端方法Register user,ScriptManager会生成一个代理函数,该函数在PageMethods中可用
我的服务器端函数有两个参数,即邮箱和密码,在这些参数之后,我们必须给出另外两个函数名,如果方法成功执行(第一个参数,即OnSuces)或方法失败(第二个参数,即result),则将运行这些函数名.
现在一切似乎都准备好了,现在我已经在我的注册按钮上添加了OnClientClick="Signup();return false;"
个.下面是我的ASPX页面的完整代码:
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<fieldset style="width: 200px;">
<asp:Label ID="lblEmailAddress" runat="server" Text="Email Address"></asp:Label>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<asp:Label ID="lblPassword" runat="server" Text="Password"></asp:Label>
<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
</fieldset>
<div>
</div>
<asp:Button ID="btnCreateAccount" runat="server" Text="Signup" OnClientClick="Signup();return false;" />
</div>
</form>
</body>
</html>
<script type="text/javascript">
function Signup() {
var email = document.getElementById('<%=txtEmail.ClientID %>').value;
var password = document.getElementById('<%=txtPassword.ClientID %>').value;
PageMethods.RegisterUser(email, password, onSucess, onError);
function onSucess(result) {
alert(result);
}
function onError(result) {
alert('Cannot process your request at the moment, please try later.');
}
}
</script>