首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何混合使用jQuery手机和ASP.NET

如何混合使用jQuery手机和ASP.NET
EN

Stack Overflow用户
提问于 2012-04-08 02:27:26
回答 1查看 8.6K关注 0票数 7

我正在制作一个移动网站,使用jQuery移动和ASP.NET 4.0网页表单。我创建了一个初始登录页面(Default.aspx),其中的按钮调用了一个ajax JavaScript函数,该函数在后台调用一个页面方法,如下所示:

Default.aspx

代码语言:javascript
复制
<%@ 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函数:

代码语言:javascript
复制
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中的页面方法

代码语言:javascript
复制
    [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中实现它们。

EN

回答 1

Stack Overflow用户

发布于 2012-05-24 01:57:58

我也是这样做的,而且效果很好。

请看下面的例子

Invoking Server Methods Using Jquery

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10057145

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档