ajax登陆实例,运用ajaxPro框架(转载)

来源:互联网 发布:matlab矩阵的转置 编辑:程序博客网 时间:2024/05/21 14:55

这是我自学ajax写的第一个作品,大家见笑,如果又感兴趣的朋友可以参考一下!使用的是ajaxPro 

<%@ Page language="c#" Codebehind="Login.aspx.cs" AutoEventWireup="false" Inherits="ajaxlogin.Login" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    
<HEAD>
        
<title>Login</title>
        
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        
<meta name="CODE_LANGUAGE" Content="C#">
        
<meta name="vs_defaultClientScript" content="JavaScript">
        
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
        
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
        
<style type="text/css">
        BODY 
{ MARGIN-TOP: 0px; MARGIN-LEFT: 0px }
        TABLE 
{ BORDER-RIGHT: #ff9966; BORDER-TOP: #ff9966; FONT-SIZE: 12px; BORDER-LEFT: #ff9966; WIDTH: 35%; BORDER-BOTTOM: #ff9966; HEIGHT: 138px; BACKGROUND-COLOR: #00ccff; align: center; cellpadding: 0; cellspacing: 0 }
        .lg 
{ FONT-SIZE: 14px; COLOR: #ffff33 }
        
</style>
        
<script language="jscript">
            
function Check()
            
{
                
var name=document.getElementById("Txtname").value;
                
var pwd=document.getElementById("Txtpwd").value;
                ajaxlogin.Login.GetCode(name,pwd,CheckUser_CallBack);
            }

            
function CheckUser_CallBack(res)
            
{
                
var value=res.value;
                
if(value=="1")
                
{
                    window.alert(
"您输入的用户名或密码不正确!");
                }

                
if(value=="2")
                
{
                    alert(
"请输入用户名!");
                }

                
if(value=="3")
                
{
                    alert(
"请输入密码!");
                }

                
if(value=="0")
                
{
                    window.open(
"welcome.aspx","_self");
                }

            }

        
</script>
    
</HEAD>
    
<body MS_POSITIONING="GridLayout">
        
<form id="Form1" method="post" runat="server">
            
<table id="lgtbn" style="Z-INDEX: 101; LEFT: 312px; POSITION: absolute; TOP: 160px">
                
<TR>
                    
<TD vAlign="middle" align="center" bgColor="#6699cc" colSpan="2">
                        
<class="lg">用 户 登 录</P>
                    
</TD>
                
</TR>
                
<TR>
                    
<TD vAlign="middle" align="center">请输入姓名:</TD>
                    
<TD>&nbsp;
                        
<asp:TextBox id="Txtname" runat="server" Width="150px"></asp:TextBox></TD>
                
</TR>
                
<TR>
                    
<TD vAlign="middle" align="center">请输入密码:</TD>
                    
<TD>&nbsp;
                        
<asp:TextBox id="Txtpwd" runat="server" TextMode="Password" Width="150px"></asp:TextBox></TD>
                
</TR>
                
<TR>
                    
<TD vAlign="middle" align="center" colSpan="2"><INPUT id="BtnSend" type="button" value="提交" onclick="Check()">&nbsp;<FONT face="宋体">
                            
<INPUT id="BtnReg" type="button" value="注册"> </FONT>
                    
</TD>
                
</TR>
            
</table>
        
</form>
    
</body>
</HTML>

后台代码 :

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace ajaxlogin
{
    
/// <summary>
    
/// Login 的摘要说明。
    
/// </summary>

    public class Login : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.TextBox Txtname;
        
protected System.Web.UI.WebControls.TextBox Txtpwd;
        SqlConnection conn 
= new SqlConnection("server=.;database=WangYue;uid=sa;pwd=");
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            AjaxPro.Utility.RegisterTypeForAjax(
typeof(Login));
            
// 在此处放置用户代码以初始化页面
        }


        
Web 窗体设计器生成的代码

        [AjaxPro.AjaxMethod]
        
public string GetCode(string name,string pwd)
        
{
            
if(name=="")
            
{
                
return "2";//用户名不能为空
            }

            
if(pwd=="")
            
{
                
return "3";//密码不能为空
            }

            SqlCommand comm 
= new SqlCommand();
            comm.Connection 
= conn;
            comm.CommandText 
= "select * from UserInfo where name='" + name + "' and pwd='" + pwd + "'";
            SqlDataAdapter da 
= new SqlDataAdapter();
            da.SelectCommand 
= comm;
            DataTable dt 
= new DataTable();
            da.Fill(dt);
            
if(dt.Rows.Count == 0)
            
{
                
return "1";//用户名或密码不正确
            }

            
return "0";
        }

    }

}

webconfig:

<httpHandlers>
   <add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory, AjaxPro" />
  </httpHandlers>

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1620576

 
原创粉丝点击