如何混合jQuery Mobile和ASP.NET

我正在使用jQuery Mobile和ASP.NET 4.0 Webforms创建一个移动网站.我创建了一个初始登录页面(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>

Ajax 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中实现.

解决方法

我正以同样的方式做事并且完美无缺.

请看一下这个例子

Invoking Server Methods Using Jquery

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: &lt;span id=&quot...
jQuery 添加水印 &lt;script src=&quot;../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...