当前位置导航:炫浪网>>网络学院>>网页制作>>ASP.NET教程

实现无刷新注册验证

  实现无刷新注册验证(利用ajax和webservice) 利用ajax和webservice实现异步无刷新的用户信息检测

  只是演示了用户注册检测功能,具体的注册功能尚未实现

  知识要点:

  想要在页面里JS代码里onclick去调用后台文件中的一个方法,搞了半天,才弄懂怎么做。

  原来是通过PageMethods来实现的。

  举个列子:

  Default.aspx 里代码

  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

  <!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>

  <script type="text/javascript" language="javascript">

  <!--

  function minbzdm()

  {

  PageMethods.OK(xxx);

  }

  function xxx(result)

  {

  alert(result);

  }

  //-->

  </script>

  </head>

  <body>

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

  <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">

  </asp:ScriptManager>

  <div>

  <input type='button' value='删除' onclick='minbzdm()' />

  </div>

  </form>

  </body>

  </html>

  Default.aspx.cs里的代码

  public partial class _Default : System.Web.UI.Page

  {

  protected void Page_Load(object sender, EventArgs e)

  {

  }

  [System.Web.Services.WebMethod]

  public static string OK()

  {

  return "OK";

  }

  }

  通过PageMethods方法来实现JS调用CS,必须注意一下几点:

  【1】静态的方法

  public static

  【2】需要在cs方法上加上:

  [System.Web.Services.WebMethod]

  【3】需要自定义一个函数接受结果

  function xxx(result)

  {

  alert(result);

  }

  【4】ScriptManager 必须设置成  EnablePageMethods="true"

相关内容
赞助商链接