Simple Ajax Login

Source: Internet
Author: User
JS part (default. aspx)
<SCRIPT>
VaR XMLHTTP;
Function createxmlhttprequest ()
{
If (window. XMLHttpRequest)
{
XMLHTTP = new XMLHttpRequest ();
} Else if (window. activexobject)
{
XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
}
}
Function login ()
{
Createxmlhttprequest (); // create a component
VaR name = Document. getelementbyid ("<% = txtname. clientid %>"). value;
VaR Password = Document. getelementbyid ("<% = txtpwd. clientid %>"). value;
VaR url = "login. aspx? Name = "+ name +" & Password = "+ password;
XMLHTTP. Open ("get", URL, true); // initialize data
XMLHTTP. onreadystatechange = iscallback; // sets the callback function.
XMLHTTP. Send (null );
}
Function iscallback ()
{
If (XMLHTTP. readystate = 4 & XMLHTTP. Status = 200 & XMLHTTP. responsetext = "true ")
{
Document. getelementbyid ("txtmsg"). value = "success ";
}
Else
Document. getelementbyid ("txtmsg"). value = "fail ";
}
</SCRIPT>

// Body part (default. aspx)
<Body>
<Form ID = "form1" runat = "server">
<Div>
Username: & nbsp;
<Asp: textbox id = "txtname" runat = "server"> </ASP: textbox> <br/>
Passowrd: & nbsp; <asp: textbox id = "txtpwd" runat = "server"> </ASP: textbox> <br/>
& Nbsp;
<Input id = "button2" onclick = "login ()" type = "button" value = "login"/> & nbsp; <br/>
& Nbsp;
<Input id = "txtmsg" type = "text"/> </div>
</Form>
</Body>

// Processing part (login. aspx. CS)
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
String name = request. querystring ["name"];
String Pwd = request. querystring ["password"];
If (name! = "" | Name! = "Null" | PWD! = "" | PWD! = "Null ")
{

Int result = geruser (name, PWD );
If (result> 0)
{
Response. Write ("true ");
Response. End ();
}
}
}
}
Public static string constr = @ "Server = (local); database = tengda; trusted_connection = yes ";
Public static sqlconnection connection;
Public static sqlconnection getconnection
{
Get
{
If (connection = NULL)
{
Connection = new sqlconnection (constr );
Connection. open ();
}
Else if (connection. State = system. Data. connectionstate. Closed)
{
Connection. open ();
}
Else if (connection. State = system. Data. connectionstate. Broken)
{
Connection. Close ();
Connection. open ();
}
Return connection;
}
}
//
Public static int geruser (string name, string PWD)
{
String SQL = "select * From role where rolename = '" + name + "' and roleremark = '" + PWD + "'";
Sqlcommand cmd = new sqlcommand (SQL, getconnection );
Return convert. toint32 (CMD. executescalar ());
}

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.