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 ());
}