如何搭配jQuery Mobile的和ASP.NET(How to mix jQuery Mobil

2019-07-28 21:05发布

我正在做一个移动网站,使用jQuery Mobile和ASP.NET 4.0 Web表单。 我已经作出的初始登录页面(Default.aspx的),其中按钮调用一个AJAX JavaScript函数,它调用在像这样的后端一个页面方法:

Default.aspx的

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="UCP.Default" %>
<!DOCTYPE html>
<html>
<head>
    <title>UA Cover Plus</title>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" />
    <script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js"></script>
</head>
<body>
    <!-- Login -->
    <div data-role="page" id="login">
        <div data-role="header" data-position="fixed">
           <h1>Login</h1>
            <a href="#Family" data-icon="gear" class="ui-btn-right" data-iconpos="notext" data-theme="b" >Options</a>
        </div>
       <div data-role="content" data-theme="a">   
            <input type="text" id="txtUserName" placeholder="Username" />
            <input type="password" name="passwordinput" id="txtPassword"  placeholder="Password" value=""  />
            <a href="javascript:Authenticate();" data-role="button" data-icon="check" data-iconpos="right">Log Me In!</a>
       </div><!-- /content -->
    </div><!-- /page -->
</body>

阿贾克斯jQuery函数:

function Authenticate() {
    $.ajax({
        type: "POST",
        url: "Default.aspx/Authenticate",
        data: "{'name': '" + $('#txtUserName').val() + "','pass': '" + $('#txtPassword').val() + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d != '-1') {
                userId = msg.d;
                GetCustomisedConsole();
            } else {
                alert("Authentication Failed");
            }
        },
        error: function () {
            alert("Error :(");
        }
    });
};

在Default.aspx.cs页面方法

    [WebMethod]
    public static int Authenticate(string name, string pass)
    {
        using (DbContext db = new DbContext())
        {
            var user = db.Users.Where(x => x.UserName == name && x.Password == pass).SingleOrDefault();
            if (user != null)
            {
                return user.Id;
            }
            else
            {
                return -1;
            }
        }
    }

我的问题是,这是它的意思做的方式? 我问这个,因为我在jQuery的例子中看到使用表格并提交,我不知道如何在asp.net实现。

Answer 1:

我在做同样的方式和运行完美。

请看看这个例子

调用服务器方法使用jQuery



文章来源: How to mix jQuery Mobile and ASP.NET