For details about logon, refer to the logon interface of a Forum:
You can use cookies to remember this information. For more information about Cookie applications, see
Http://jb51.net/article/33590.htm
Http://jb51.net/article/33591.htm
Now we can simulate a logon interface:
Copy codeThe Code is as follows:
<Table>
<Tr>
<Td style = "width: 15%; text-align: right;">
User Name
</Td>
<Td>
<Asp: TextBox ID = "TextBoxUserName" runat = "server"> </asp: TextBox>
</Td>
</Tr>
<Tr>
<Td style = "text-align: right;">
Password
</Td>
<Td>
<Asp: TextBox ID = "TextBoxPassword" TextMode = "Password" runat = "server"> </asp: TextBox>
</Td>
</Tr>
<Tr>
<Td style = "text-align: right;">
Remember me
</Td>
<Td>
<Asp: CheckBox ID = "CheckBoxRememberMe" runat = "server"/>
</Td>
</Tr>
<Tr>
<Td style = "text-align: right;">
</Td>
<Td>
<Asp: Button ID = "ButtonLogin" runat = "server" Text = "Login" OnClick = "ButtonLogin_Click"/>
</Td>
</Tr>
</Table>
Running effect:
We need to determine whether the user selects the Remember me CheckBox when clicking the button. If so, we need to record the login information to the Cookie, set the Cookie expiration time to expire seven days later. Otherwise, only the login information is recorded in the Cookie, and the Cookie expiration time is not set. You can refer to the following logon Event code:
Copy codeThe Code is as follows:
Protected void ButtonLogin_Click (object sender, EventArgs e)
{
Response. Cookies ["Name"]. Expires = DateTime. Now. AddDays (-1 );
Response. Cookies ["Password"]. Expires = DateTime. Now. AddDays (-1 );
If (CheckBoxRememberMe. Checked)
{
Response. Cookies ["Name"]. Expires = DateTime. Now. AddDays (7 );
Response. Cookies ["Password"]. Expires = DateTime. Now. AddDays (7 );
}
Response. Cookies ["Name"]. Value = this. TextBoxUserName. Text. Trim ();
Response. Cookies ["Password"]. Value = this. TextBoxPassword. Text. Trim ();
}
Next, you have to read the Cookie in Page_load.
Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
If (Request. Cookies ["Name"]! = Null & Request. Cookies ["Password"]! = Null)
{
This. TextBoxUserName. Text = Request. Cookies ["Name"]. Value;
This. TextBoxPassword. Attributes ["value"] = Request. Cookies ["Password"]. Value;
}
}
}
Check the operation demo. There are three states in the demo. The first is that you do not click CheckBox. In this way, close the window and you will not remember the logon information when you open it again.
The second is to select the CheckBox, so that the next time you open the window, you can also see that the account and password are stored in the corresponding text box, which means the Cookie has not expired.
Third, Click Log On again without clicking the remember me CheckBox. In this way, the system removes the Cookie: