loner_li JQuery+AJax 实现简单的用户注册

web服务文件代码

 [WebMethod]
        public int ExecuteInsert(string name,string password)
        {
            string constr = ConfigurationManager.ConnectionStrings["SqlAjaxReg"].ConnectionString;
           
          
            SqlConnection con = new SqlConnection(constr);
            con.Open();
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandText = "insert into  T_User(UserName,PassWord)values(@name,@pwd) ";
            cmd.Parameters.AddWithValue("@name", name);
            cmd.Parameters.AddWithValue("@pwd", password);
           
          
            return cmd.ExecuteNonQuery();
        }
   
显示页

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="js/Jquery1.7.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $('#btnReg').click(function () {
             
                $.ajax({
                    type: "post",
                    contentType: "application/json",
                    url: "WebService1.asmx/ExecuteInsert",
                    data: "{name:'" + $('#txtUserName').val() + "',password:'" + $('#txtPassword').val() + "'}",
                    success: function (result) {
                        alert("成功注册" + result.d + "条数据。");
                    }
                }
                );
            });
        })
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        用户名:<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox><br />
        密码 :&nbsp;
        <asp:TextBox ID="txtPassword" TextMode="Password" runat="server"></asp:TextBox>
        <br />
        <br />
      
        <input id="btnReg" type="button" value="注册" />
    </div>
    </form>
</body>
</html>

web.config文件
<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
  <connectionStrings>
    <add name="SqlAjaxReg" connectionString="Data Source=.;Initial Catalog=School;User ID=xx;Password=xxxx;"/>
  </connectionStrings>
</configuration>

你可能感兴趣的:(loner_li JQuery+AJax 实现简单的用户注册)