Simple Ajax client login verification

Source: Internet
Author: User
I will not blow off the ease of server operations. Everyone on the Earth knows that it is the most annoying thing to do is page refresh, and his head is dizzy, and when he is refreshing, also trigger server-side events (solution: http://skylaugh.cnblogs.com/archive/2006/06/05/418010.html), now the emergence of Ajax, their combination is the inevitable development!

I. Introduction to the basic use of Ajax in Asp. Net
1. Introduce the Ajax. dll file in the project.
Ajax. dll implementation XmlHttpRequest request server implementation details .. In the. net project, add a reference to it

The rows are encapsulated.

2. Set HttpHandle in web. config
<HttpHandlers>
<Add verb = "POST, GET" path = "ajax/*. ashx" type = "Ajax. PageHandlerFactory, Ajax"/>
</HttpHandlers>
3. Add some references between <HEAD> and </HEAD> as follows:
<Script src = js/Xml. js> </script>
<Link href = "css/myStyle.css" type = "text/css" rel = "stylesheet">
<Script src = "/HttpForAjax/ajax/common. ashx" type = "text/javascript"> </script>
<Script src = "/HttpForAjax/ajax/Ttyu. AjaxData, HttpForAjax. ashx" type = "text/javascript"> </script>

II. Introduction topic-user login verification
1. Foreground Html:
<Form id = "Form1" method = "post" runat = "server" action = "" onsubmit = "login. GetLogin (); return false;">
<TABLE id = "Table1" cellSpacing = "1" cellPadding = "1" width = "300" border = "1">
<TR>
<TD> </TD>
<TD> <INPUT type = "text" id = "txtUsername"> usename </TD>
</TR>
<TR>
<TD> </TD>
<TD> <INPUT type = "password" id = "txtPassword"> pwd </TD>
</TR>
<TR>
<TD> </TD>
<TD> <INPUT type = "submit" value = "login"> </TD>
</TR>
</TABLE>
</Form>

2. Reference Js files

<SCRIPT language = "javascript" src = "login. js" type = "text/javascript"> </SCRIPT>
<Script language = "javascript">
Window. onload = function ()
{
Login = new Login (testAjax );
}
</Script>

Login. Js File
// Extract the Control Value
Function getValueById (pObjID ){
Var obj = document. getElementById (pObjID );
Try {
Return obj. value;
} Catch (e ){
Alert ("control:" + pObjID + "does not exist, or there is no value attribute ");
}
}

Function Login (obj)
{
This. OBJ = obj;
This. GetLogin = function ()
{
Var returnValue;
Var username = getValueById ('txtusername ');
Var password = getValueById ('txtpassword ');
If (! Username |! Password)
{
Alert ('enter your username and password! ');
Return;
}
Try
{
ReturnValue = this. OBJ. Login (username, password). value;
} Catch (e)
{
Alert ('logon error, please try again later or contact the postmaster ');
}
Switch (returnValue)
{

Case 1:
Alert ('Sorry, the user name or password you entered is incorrect or it is not an administrator! ');
Break;
Case 0:
Alert ('administrator logon successful! ');
Specified parameter Doc ument. location. href ('../Error. aspx ');
Break;
Default:
Alert ('logon failed. Please try again later or contact the postmaster '+ returnValue );
Break;
}
}
}

3.. cs File

Private void Page_Load (object sender, System. EventArgs e)
{
Ajax. Utility. RegisterTypeForAjax (typeof (testAjax ));
}

[Ajax. AjaxMethod ()]
Public int Login (string username, string password)
{
// Administrator Logon Portal
Action. Common. CDB cdb = new Action. Common. CDB ();
If ("admin" = cdb. ExeScalar ("select upower from users where

Uname = '"+ username +"' and upwd = '"+ password + "'"))
Return 0;
Else
Return 1;
}

Original post: http://www.cnblogs.com/skylaugh/archive/2006/06/14/426029.aspx

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.