页面异步获取数据

页面regist.aspx

<script type="text/javascript">
function CheckEmail(obj) {
    var email = obj.value;
    $.ajax({
        type: "POST",
        url: "regist.aspx", //提交给自己
        data: { "checkmail": email},
        timeout: 10000,
        success: function(data) {
                var myinfo = eval('(' + data + ')');
                CheckEmailComplete(myinfo.info);
            }
    });    
}


页面代码regist.aspx.cs

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request["checkmail"] != null)
            {//异步获取
                ReturnJson();
                return;
            }
        }
}
    string CheckMail(string mail)
    {
        string mymail = mail.Trim();

        if(mymail.Length == 0) return "请输入邮箱";

        Regex rm = new Regex(@"\w+(?:[-+.]\w+)*@\w+(?:[-.]\w+)*\.\w+(?:[-.]\w+)*",RegexOptions.IgnoreCase);
        if (!rm.IsMatch(mymail))
        {
            return "邮箱格式不对";
        }

        SqlParameter[] sqlParas = { new SqlParameter("@UserEmail", mymail) };
        string sql =@"SELECT TOP 1 WebUserId FROM WebUser WHERE UserEmail=@UserEmail";
        if (SQLHelper.DBHelper.ExecuteScalar(CommandType.Text, sql, sqlParas) != null)
        {
            return "已被注册,<a href='Login.aspx' class='blue'><u>登录</u>?</a>";
        }

        return "";
    }
    void ReturnJson()
    {
        string info = CheckMail(Request["checkmail"]);
        string json = String.Format(@"{{""info"":""{0}""}}", info);
        Response.Clear();
        Response.Write(json);
        Response.End();
    }


参考文章:

http://www.cnblogs.com/TerryFeng/archive/2009/01/30/1381431.html

posted on 2011-07-08 11:21  左直拳  阅读(283)  评论(0编辑  收藏  举报

导航