'How to mix jQuery Mobile and ASP.NET
I'm making a mobile website, using jQuery Mobile and ASP.NET 4.0 Webforms. I have made an initial login page (Default.aspx) in which the button calls an ajax JavaScript function, which calls a page method in the backend like so:
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 :
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 :(");
}
});
};
Page method in 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;
}
}
}
My question is, is this the way its meant to be done? I ask this because I see in the jQuery examples the use of forms and submits, which I have no idea how to implement in asp.net.
Solution 1:[1]
I'm doing in the same way and works perfect.
Please take a look to this example
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | McGarnagle |