Asp. NET Web site management system exit clear browser cache, session code _ Practical skills

Source: Internet
Author: User
Tags browser cache clear browser cache
1, in the system Landing Success Record login username, password and other information (login function part of the code)
Copy Code code as follows:

session["id"] = user.id.ToString ();
session["name"] = User.name.ToString ();
session["pwd"] = user.password.ToString ();
session["Time" = user. Logintime.tostring ();
session["authority"] = user.limits.ToString ();

2. Add the following code to each page of the management system to determine whether the session value is empty when the page is loaded
Copy Code code as follows:

protected void Page_Load (object sender, EventArgs e)
{
if (session["id"] = = NULL | | session["name"] = = NULL | | session["time"] = = NULL | | session["authority"] = = NULL | | session["pwd"] = = null)
Response.Redirect ("~/login.aspx", true);
if (! IsPostBack)
{
。。。。。。
}
}

3, in the Click "Exit System" execution of the event to add the session empty code and browser cache empty code
Copy Code code as follows:

public void clear (object sender, EventArgs e)
{
session["id" = NULL;
session["name" = null;
Clearclientpagecache ();
Response.Redirect ("~/login.aspx");
}
public void Clearclientpagecache ()
{
Clear browser Cache
Response.Buffer = true;
Response.ExpiresAbsolute = DateTime.Now.AddDays (-1);
Response.Cache.SetExpires (DateTime.Now.AddDays (-1));
Response.Expires = 0;
Response.CacheControl = "No-cache";
Response.Cache.SetNoStore ();
}

Because my "Exit system" is written in the master page with HTML <a> tags, the above code is written in the. cs file of the master page.
Master Page code:
Copy Code code as follows:

<a class= "atop" target= "_self" <span style= "Background-color: #ff0000" >runat= "Server" onserverclick</ span> = "Clear" > Exit system </a>

================================================================================================
The previous version has been unable to achieve the function, tangled for a long time, did not find out the problem, the just started to carry out the wrong code posted, but also I think the wrong place to paste out, I hope everyone criticized correct.
This version of the error is: Landing after the successful entry to the main page, and then click into other pages can not enter, will jump to the landing interface.
My thoughts:
1, I found in the track debugging, every time the page load will automatically execute the master page. cs file Clear () method, so you can not through other pages Page_Load () method of the IF (session["id"] = NULL | | session["name"] = = NULL | | session["time"] = = NULL | | session["authority"] = = NULL | | session["pwd"] = = null) Response.Redirect ("~/login.aspx", true);
2, my question is that the clear () method is clearly clicked before the execution, why every time the page load automatically executed?
3, I consider the cause of the error is the client and server-side implementation of the different methods, and then found on the internet on the onclick, and onserverclick differences, but their understanding is not very clear. I hope you can communicate.
The difference between onclick and OnServerClick see also: http://www.jb51.net/article/30313.htm
The front desk code is the first (master page foreground code):
Copy Code code as follows:

<a href= "~/login.aspx" class= "atop" target= "_self" onclick= "Clear ()" > Account information </a>
<script>
function Clear () <BR> {<br> <%clear ();%><br>}
</script>

Master Page Background Code
Copy Code code as follows:

public void Clear ()
{
session["id" = NULL;
session["name" = null;
}
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.