简体   繁体   English

如何在页面视图或按钮单击之间增加ASP.NET(C#)中的变量

[英]How to increment a variable in ASP.NET (C#) between page views or button clicks

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. 我已经尝试在http://msdn.microsoft.com/en-us/magazine/cc300437.aspx和谷歌上关注指南,但我看不到我没做的。

I have some very basic code which i have written just trying to get this to work: 我有一些非常基本的代码,我写的只是试图让它工作:

The Default.aspx code: Default.aspx代码:

<%@ 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: Default.aspx.cs代码:

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. 我每次都得1分。

NB this is my first asp.net project, also i'm fairly new to c#. 注意,这是我的第一个asp.net项目,我也是c#的新手。

Page_Load is ran every postback as well as the initial load. 每次回发都会运行Page_Load以及初始加载。 You need to specify no postback in your Page_Load : 您需要在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. 只是为了澄清,如果没有设置值,最好只设置值是每次有人直接访问页面时Page.IsPostBack为false。 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. 比如你有你的页面http://example.com/Demo/Default.aspx ,在顶部你有一个徽标,你在这里包装徽标 ,每次有人点击徽标时会重置会话,即使他们实际上并没有离开页面。 Also happens if they refresh on their browser without re-posting the last post. 如果他们在浏览器上刷新而没有重新发布最后一篇文章也会发生。

Read on MSDN : Page.IsPostBack Property - Gets a value that indicates whether the page is being rendered for the first time or is being loaded in response to a postback. 阅读MSDN: Page.IsPostBack属性 - 获取一个值,该值指示页面是第一次呈现还是正在加载以响应回发。

vlue of property is true if the page is being loaded in response to a client postback; 如果正在加载页面以响应客户端回发,则vlue属性为true ; otherwise, false . 否则, 错误

code like this ...you need to put the code in !IsPostBack as like below 像这样的代码......你需要把代码放进去!IsPostBack如下所示

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

serverside control generate postback to page it self so the code that you dont wnat to execute on each postbck need to be put as above 服务器端控件生成回发以自行分页,因此您不希望在每个postbck上执行的代码需要如上所述

this will resolve your issue easily... 这将很容易解决您的问题...

further to this you can create static property for count like this 此外,您可以为此计数创建静态属性

Check my post on : Programming Practice for Server Side State Maintenance Variable 查看我的帖子: 服务器端状态维护变量的编程实践

private int ClickCount
{
  get 
   {
     if (Session["clickcount"] == null)
     {         Session["clickcount"] = 0; return 0;      }
     else 
        return (int)Session["clickcount"] ; 
   }
   set
   {
      Session["clickcount"] = value;
   }
} 

than in final code 而不是最终的代码

protected void Page_Load(object sender, EventArgs e)     
{
         if(!IsPostBack)
        {
            ClickCount = 0; 
        }
}

protected void lnkClickButton_Click(object sender, EventArgs e)
{
              int val = ClickCount ;
              ClickCount  = val + 1; 
}

Writing: 写作:

Session["clickcount"] = 0;

In the Page_Load will cause the counter to be reset every time the user enters the page . 在Page_Load中, 每次用户进入页面时都会重置计数器。

Seems to me you want something like this: 在我看来你想要这样的东西:

protected void lnkClickButton_Click(object sender, EventArgs e)
{
    if (Session["clickcount"] == null)
    {
            Session["clickcount"] = 1;
    }
    else
    {
           Session["clickscount"] = (int)Session["clickscount"] + 1;
    }

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

You get 1 because every post back your session and cache variable equal to 0. 你得到1,因为你的会话和缓存变量的每个帖子都等于0。

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

And button click occur after the page_load, So you should use IsPostback property. 并且在page_load之后发生按钮单击,因此您应该使用IsPostback属性。

 protected void Page_Load(object sender, EventArgs e)     

{ {

 if(!IsPostBack)
{
     Session["clickcount"] = 0;
     Cache["clickscount"] = 0;
}

} }

Now these variables initialize only when page is loaded. 现在,这些变量仅在加载页面时初始化。

You should go through following link. 你应该通过以下链接。 It is described Asp.net page life. 它描述了Asp.net的页面生活。

http://msdn.microsoft.com/en-us/library/ms178472.aspx http://msdn.microsoft.com/en-us/library/ms178472.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM