Front-end code:
<Head>
<Title> </title>
<Style type = "text/css">
Body {font: 12px Helvetica, Arial, sans-serif; margin: 100px auto; padding: 0; word-wrap: break-word; word-break: break-all; background-color: #016BA9 ;}
. Login1 {background-image: url (../Images/login_1.jpg); width: 960px; height: 94px; margin: 0 auto ;}
. Login2 {background-image: url (../Images/login_2.jpg); width: 960px; height: 49px; margin: 0 auto ;}
. Login3 {background-image: url (../Images/login_3.jpg); width: 960px; height: 125px; margin: 0 auto ;}
. Login4 {background-image: url (../Images/login_4.jpg); width: 960px; height: 91px; margin: 0 auto ;}
. LoginTXT {border: 1px solid #800000; width: 150px; background-color: #866961; color: # fff; height: 19px ;}
</Style>
<Script type = "text/javascript">
If (top. location! = Self. location)
Top. location = self. location;
Function CheckIn (){
Var names = document. getElementById ("txtU_Name"). value;
Var pass = document. getElementById ("txtU_Pass"). value;
Var check = document. getElementById ("txtU_Check"). value;
If (names = ""){
Alert ("Enter the user name ...");
Document. getElementById ("txtU_Name"). focus ();
Return false;
}
Else if (pass = ""){
Alert ("enter your password ...");
Document. getElementById ("txtU_Pass"). focus ();
Return false;
}
Else if (check = ""){
Alert ("Enter the verification code ...");
Document. getElementById ("txtU_Check"). focus ();
Return false;
}
}
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server" defaultfocus = "txtU_Name" defaultbutton = "BtnLogin">
<Div class = "login1"> </div>
<Div class = "login2"> </div>
<Div class = "login3">
<Table cellpadding = "0" cellspacing = "1" border = "0" style = "margin-left: 320px; width: 300px;">
<Tr>
<Td> User name: </td>
<Td> <asp: textbox ID = "txtU_Name" runat = "server" class = "loginTXT" maxlength = "20"> </asp: textbox> </td>
</Tr>
<Tr>
<Td> User Password: </td>
<Td> <input id = "txtU_Pass" type = "password" runat = "server" class = "loginTXT" maxlength = "6"/> </td>
</Tr>
<Tr>
<Td> Verification Code: </td>
<Td> <asp: textbox ID = "txtU_Check" runat = "server" class = "loginTXT" style = "width: 85px;" maxlength = "5"/> & nbsp; </td>
</Tr>
<Tr>
<Td colspan = "2"> </Tr>
<Tr>
<Td> </td>
<Td>
<Asp: button ID = "BtnLogin" runat = "server" text = "Logon management" OnClientClick = "return CheckIn ()" onclick = "BtnLogin_Click"/>
& Nbsp; <asp: button ID = "BtnHome" runat = "server" text = "back to homepage"
Onclick = "BtnHome_Click"/>
</Td>
</Tr>
</Table>
</Div>
<Div class = "login4"> </div>
</Form>
</Body>
</Html>
Background code:
Protected void BtnLogin_Click (object sender, EventArgs e)
{
String username = txtU_Name.Text;
String pwd = txtU_Pass.Value;
String check = txtU_Check.Text;
If (CheckNull ())
{
If (check. ToString (). ToLower () = Session ["ValidateCode"]. ToString (). ToLower ())
{
String strsql = "select count (1) from Login where UserName = '" + username + "' and UserPwd = '" + pwd + "'";
Object obj = SqlHelper. ExecuteScalar (ConfigurationManager. ConnectionStrings ["conn"]. ToString (), CommandType. Text, strsql );
If (obj! = Null & (int) obj> 0)
{
Response. Write ("<script> alert ('logon successful! ') </Script> ");
Session ["username"] = username;
HttpCookie hc = new HttpCookie ("username", username );
Hc. Expires = DateTime. Now. AddMinutes (1 );
Response. Cookies. Add (hc );
Response. Redirect ("Main. aspx ");
}
Else
{
Response. Write ("<script> alert ('user name or password is incorrect! ') </Script> ");
Response. Write ("<script> window. location. href = 'login. aspx '</script> ");
}
}
Else
{
Response. Write ("<script> alert ('verification code error! ') </Script> ");
Response. Write ("<script> window. location. href = 'login. aspx '</script> ");
}
}
}
Private bool CheckNull ()
{
Return (! (String. IsNullOrEmpty (txtU_Name.Text.Trim ()))&&! (String. IsNullOrEmpty (txtU_Pass.Value.Trim ()))&&! (String. IsNullOrEmpty (txtU_Check.Text.Trim ())))? True: false;
}
Main homepage code: page_Load
Try
{
String username = Session ["username"]. ToString ();
}
Catch
{
Response. Write ("<script> alert ('Log On from the homepage! '); Location. href = 'login. aspx'; </script> ");
}
This article from the "cainiao" blog, please be sure to keep this source http://wolfoo.blog.51cto.com/3126463/1288217