Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7995931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:34:01+00:00 2026-06-04T14:34:01+00:00

I have the feeling that i’m missing something key here. I’ve tried following guides

  • 0

I have the feeling that i’m missing something key here.

I’ve tried following guides on http://msdn.microsoft.com/en-us/magazine/cc300437.aspx
and on google, but i can’t see what i haven’t done.

I have some very basic code which i have written just trying to get this to work:

The Default.aspx code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" EnableSessionState="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Demo Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="myLabel" runat="server" Text="foo"></asp:Label>
        <asp:LinkButton ID="lnkClickButton" runat="server" OnClick="lnkClickButton_Click" CommandName="Clicky">Click Me</asp:LinkButton>
    </div>
    </form>
</body>
</html>

The Default.aspx.cs code:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["clickcount"] = 0;
        Cache["clickscount"] = 0;
    }
    protected void lnkClickButton_Click(object sender, EventArgs e)
    {
        Session["clickcount"] = (int)Session["clickcount"] + 1;
        Cache["clickscount"] = (int)Cache["clickscount"] + 1;

        Label myLabel = ((Label)this.FindControl("myLabel"));
        if (myLabel != null)
        {
            myLabel.Text = "Session: " + Session["clickcount"] + "; Cache: " + Cache["clickscount"] + ";";
        }
    }
}

I’ve tried using both the session object and the cache object to increment the values, but to no avail. I just get 1 every time.

N.B. this is my first asp.net project, also i’m fairly new to c#.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T14:34:03+00:00Added an answer on June 4, 2026 at 2:34 pm

    Page_Load is ran every postback as well as the initial load. You need to specify no postback in your Page_Load:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack){
           Session["clickcount"] = 0;
           Cache["clickscount"] = 0;
        }
    }
    

    Better still, specify that it should only be set if it doesn’t already have a value:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["clickcount"] == null){
           Session["clickcount"] = 0;
        }
    }
    

    Just to clarify, the reason that it is better to only set the value if it isn’t already set is that Page.IsPostBack is false every time someone directly visits the page. Say for instance you have your page http://example.com/Demo/Default.aspx and at the top you have a logo which you wrap in logo here, the session would be reset every time somebody clicked on the logo, even though they didn’t actually leave the page. Also happens if they refresh on their browser without re-posting the last post.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a feeling that I'm missing something simple here but can't seem to
I have a feeling that I'm missing something obvious but can't seem to resolve
I have a feeling that this is a subjective question, but perhaps there's something
I have the feeling that I'm missing the obvious, but have not succeeded with
While implementing an == operator, I have the feeling that I am missing some
This is my first post and I have the feeling that this is something
I have a feeling that the answer to this is easy but I can't
I have a feeling that our .NET remoting services are getting overrun by objects
Although I have a feeling that this isn't technically possible, it's worth asking anyways.
I am relatively new to php and have a feeling that I am going

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.