I am making a mobile site using jQuery Mobile and ASP.NET 4.0 Webforms. I made the initial login page (Default.aspx), in which the button calls the JavaScript ajax function, which calls the page method in the backend, for example:
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> <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> </div> </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, should this be done so? I ask about this because I see jQuery examples using forms and views that I donβt know how to implement in asp.net.
source share