Update: the clearest explanation I have found on the web as I have been struggling through this can be found here.
更新:我在网上找到的最清晰的解释,因为我一直在努力解决这个问题,这里可以找到。
Maybe I just don't understand the model of runat server terribly well. It appears the following code is always executing the if block. If the code is running on the server side I guess I can understand that it has to be stateless.
也许我只是不太了解runat服务器的模型。看来以下代码始终执行if块。如果代码在服务器端运行,我想我可以理解它必须是无状态的。
I am a seasoned non-web programmer but it appears counter intuitive to me. Will I need to create some sort of session object or pass the current state along in the URL or what?
我是一名经验丰富的非网络程序员,但对我而言似乎有点直观。我是否需要创建某种会话对象或者在URL中传递当前状态或者什么?
<script runat="server">
DateTime begin;
DateTime end;
int iSelectedStart = 0;
int iSelectedEnd = 0;
int iPutName = 0;
protected void Button1_Click(object sender, EventArgs e)
{
if (iPutName == 0)
{
iPutName = 1;
Label1.Text = TextBox1.Text + " you will be slecting your start and end dates.";
6 个解决方案
#1
It looks like part of your code got cut off, but here's the basic thing with web programming -- it's stateless. Unless, that is, you do something (use ViewState, Session, etc.) to add some state into the mix.
看起来你的代码的一部分被切断了,但这是网络编程的基本内容 - 它是无状态的。除非,即你做某事(使用ViewState,Session等)将一些状态添加到混合中。
In your case, it looks like you want to maintain some state through refreshes of the page. Put the values you want to preserve in ViewState to keep them across postbacks to the same page. If you want to hold values across pages on your site, use Session. If you want to maintain values across visits to the site, put them in a database and tie them to a login or cookie.
在您的情况下,看起来您希望通过刷新页面来维护某些状态。将要保留的值放在ViewState中,以使它们在回发中保持同一页面。如果要在站点的页面中保存值,请使用会话。如果要在站点访问期间维护值,请将它们放在数据库中并将它们绑定到登录名或cookie。
#2
The important thing to remember here is that the web is stateless. Each request from a person's browser is completely separate from all previous requests. What's happening with your code is that the class is being instantiated from scratch each time the client requests a page. That includes clicking Button1.
这里要记住的重要一点是网络是无状态的。来自某人浏览器的每个请求都与之前的所有请求完全分开。您的代码发生的事情是,每次客户端请求页面时,都会从头开始实例化类。这包括单击Button1。
If you want values to persist between requests, you have to store it in a location where it can be retrieved later. The Session object provides this for you.
如果希望值在请求之间保持不变,则必须将其存储在稍后可以检索的位置。 Session对象为您提供此功能。
Basically, you'll need to store the iPutName variable in the session somehow. Probably the nicest way is to encapsulate it in a property:
基本上,您需要以某种方式将iPutName变量存储在会话中。可能最好的方法是将其封装在属性中:
protected int iPutName
{
get {
if (Session["iPutName"] == null)
Session["iPutName"] == 0;
return Session["iPutName"];
}
set { Session["iPutName"] = value; }
}
Edit: The ViewState object will work as well (as long as ViewState is turned on on the page). This encodes the value in a hidden field in the HTML and decodes it when it comes back.
编辑:ViewState对象也可以工作(只要在页面上打开ViewState)。这会将值编码在HTML中的隐藏字段中,并在返回时对其进行解码。
Edit (again): Apologies for repeated edits, but I should clear this up. What Jonathan and Emil have said is correct. You should probably be using ViewState for this rather than Session unless you want this value to remain accessible between pages. Note that this does require that ViewState is turned on and it will result in a larger payload being sent to the client.
编辑(再次):为重复编辑道歉,但我应该清楚这一点。乔纳森和埃米尔所说的是正确的。您可能应该使用ViewState而不是Session,除非您希望此值在页面之间保持可访问状态。请注意,这确实需要打开ViewState,这将导致将更大的有效负载发送到客户端。
#3
I really recommend to look at the quick start tutorial.
我真的建议你看一下快速入门教程。
http://quickstarts.asp.net/QuickStartv20/aspnet/Default.aspx
There are a lot of concepts in dot net to simulate state on the UI. In your case I think what you really want to do is using viewstate. Sessions should be used with care and I think the concept you are looking for i localized to the page not to the entier user session.
点网中有很多概念来模拟UI上的状态。在你的情况下,我认为你真正想做的是使用viewstate。应谨慎使用会话,并且我认为您正在寻找的概念我已本地化到页面而非本地用户会话。
You should also look at the concept codebehind/codefront as well.
您还应该查看代码隐藏/代码前端的概念。
#4
Because ASP.NET is stateless what you're assuming is correct. The postback will cause the page to lose the variable for iPutName. Storing in session is one option, or storing in viewstate could be another option.
因为ASP.NET是无状态的,所以你所假设的是正确的。回发将导致页面丢失iPutName的变量。存储在会话中是一种选择,或者存储在viewstate中可能是另一种选择。
The main thing to understand here is how the ASP.NET page lifecycle works and what happens each time you post back to the server.
这里要理解的主要内容是ASP.NET页面生命周期如何工作以及每次回发到服务器时会发生什么。
#5
For the lifecycle, check out the following URL : http://www.eggheadcafe.com/articles/20051227.asp
有关生命周期,请查看以下URL:http://www.eggheadcafe.com/articles/20051227.asp
You can store your iPutName in the session by doing this :
您可以通过执行以下操作将iPutName存储在会话中:
Session["iPutName"] = iPutName;
Fetching the session variable is also easy, but you should be sure to do a NULL check, like this:
获取会话变量也很容易,但您应该确保进行NULL检查,如下所示:
if (Session["iPutName"] != null) iPutName = Session["iPutName"];
Haven't tested any of this, but if you encounter typos... sorry ;)
没有测试任何这个,但如果你遇到错别字...对不起;)
#6
The Url you posted in your update is definitely not "All You Want to Know" about ViewState.... not even close. I only scanned his article but he doesn't appear to address Page Life Cycle at all. If your going the View State route then read these 2 links:
您在更新中发布的网址绝对不是关于ViewState的“所有您想知道的”....甚至没有关闭。我只扫描了他的文章,但他似乎根本没有解决Page Life Cycle问题。如果您进入View State路线,请阅读以下2个链接:
Understanding ASP.NET View State by Scott Mitchell
了解Scott Mitchell的ASP.NET View State
Truly Understanding ViewState By Dave Reed
真正了解ViewState作者:Dave Reed
If you're new to ViewState then dump all that guff (1 half the article) about parsing ViewState mentioned in your linked article. It's simply not required for anything but highly specialized scenarios. It is definitely not a normal thing to be doing re:ViewState.
如果您是ViewState的新手,那么请转储所有关于解析链接文章中提到的ViewState的guff(文章的一半)。除了高度专业化的场景之外,它根本不是必需的。做re:绝对不是正常的事:ViewState。
#1
It looks like part of your code got cut off, but here's the basic thing with web programming -- it's stateless. Unless, that is, you do something (use ViewState, Session, etc.) to add some state into the mix.
看起来你的代码的一部分被切断了,但这是网络编程的基本内容 - 它是无状态的。除非,即你做某事(使用ViewState,Session等)将一些状态添加到混合中。
In your case, it looks like you want to maintain some state through refreshes of the page. Put the values you want to preserve in ViewState to keep them across postbacks to the same page. If you want to hold values across pages on your site, use Session. If you want to maintain values across visits to the site, put them in a database and tie them to a login or cookie.
在您的情况下,看起来您希望通过刷新页面来维护某些状态。将要保留的值放在ViewState中,以使它们在回发中保持同一页面。如果要在站点的页面中保存值,请使用会话。如果要在站点访问期间维护值,请将它们放在数据库中并将它们绑定到登录名或cookie。
#2
The important thing to remember here is that the web is stateless. Each request from a person's browser is completely separate from all previous requests. What's happening with your code is that the class is being instantiated from scratch each time the client requests a page. That includes clicking Button1.
这里要记住的重要一点是网络是无状态的。来自某人浏览器的每个请求都与之前的所有请求完全分开。您的代码发生的事情是,每次客户端请求页面时,都会从头开始实例化类。这包括单击Button1。
If you want values to persist between requests, you have to store it in a location where it can be retrieved later. The Session object provides this for you.
如果希望值在请求之间保持不变,则必须将其存储在稍后可以检索的位置。 Session对象为您提供此功能。
Basically, you'll need to store the iPutName variable in the session somehow. Probably the nicest way is to encapsulate it in a property:
基本上,您需要以某种方式将iPutName变量存储在会话中。可能最好的方法是将其封装在属性中:
protected int iPutName
{
get {
if (Session["iPutName"] == null)
Session["iPutName"] == 0;
return Session["iPutName"];
}
set { Session["iPutName"] = value; }
}
Edit: The ViewState object will work as well (as long as ViewState is turned on on the page). This encodes the value in a hidden field in the HTML and decodes it when it comes back.
编辑:ViewState对象也可以工作(只要在页面上打开ViewState)。这会将值编码在HTML中的隐藏字段中,并在返回时对其进行解码。
Edit (again): Apologies for repeated edits, but I should clear this up. What Jonathan and Emil have said is correct. You should probably be using ViewState for this rather than Session unless you want this value to remain accessible between pages. Note that this does require that ViewState is turned on and it will result in a larger payload being sent to the client.
编辑(再次):为重复编辑道歉,但我应该清楚这一点。乔纳森和埃米尔所说的是正确的。您可能应该使用ViewState而不是Session,除非您希望此值在页面之间保持可访问状态。请注意,这确实需要打开ViewState,这将导致将更大的有效负载发送到客户端。
#3
I really recommend to look at the quick start tutorial.
我真的建议你看一下快速入门教程。
http://quickstarts.asp.net/QuickStartv20/aspnet/Default.aspx
There are a lot of concepts in dot net to simulate state on the UI. In your case I think what you really want to do is using viewstate. Sessions should be used with care and I think the concept you are looking for i localized to the page not to the entier user session.
点网中有很多概念来模拟UI上的状态。在你的情况下,我认为你真正想做的是使用viewstate。应谨慎使用会话,并且我认为您正在寻找的概念我已本地化到页面而非本地用户会话。
You should also look at the concept codebehind/codefront as well.
您还应该查看代码隐藏/代码前端的概念。
#4
Because ASP.NET is stateless what you're assuming is correct. The postback will cause the page to lose the variable for iPutName. Storing in session is one option, or storing in viewstate could be another option.
因为ASP.NET是无状态的,所以你所假设的是正确的。回发将导致页面丢失iPutName的变量。存储在会话中是一种选择,或者存储在viewstate中可能是另一种选择。
The main thing to understand here is how the ASP.NET page lifecycle works and what happens each time you post back to the server.
这里要理解的主要内容是ASP.NET页面生命周期如何工作以及每次回发到服务器时会发生什么。
#5
For the lifecycle, check out the following URL : http://www.eggheadcafe.com/articles/20051227.asp
有关生命周期,请查看以下URL:http://www.eggheadcafe.com/articles/20051227.asp
You can store your iPutName in the session by doing this :
您可以通过执行以下操作将iPutName存储在会话中:
Session["iPutName"] = iPutName;
Fetching the session variable is also easy, but you should be sure to do a NULL check, like this:
获取会话变量也很容易,但您应该确保进行NULL检查,如下所示:
if (Session["iPutName"] != null) iPutName = Session["iPutName"];
Haven't tested any of this, but if you encounter typos... sorry ;)
没有测试任何这个,但如果你遇到错别字...对不起;)
#6
The Url you posted in your update is definitely not "All You Want to Know" about ViewState.... not even close. I only scanned his article but he doesn't appear to address Page Life Cycle at all. If your going the View State route then read these 2 links:
您在更新中发布的网址绝对不是关于ViewState的“所有您想知道的”....甚至没有关闭。我只扫描了他的文章,但他似乎根本没有解决Page Life Cycle问题。如果您进入View State路线,请阅读以下2个链接:
Understanding ASP.NET View State by Scott Mitchell
了解Scott Mitchell的ASP.NET View State
Truly Understanding ViewState By Dave Reed
真正了解ViewState作者:Dave Reed
If you're new to ViewState then dump all that guff (1 half the article) about parsing ViewState mentioned in your linked article. It's simply not required for anything but highly specialized scenarios. It is definitely not a normal thing to be doing re:ViewState.
如果您是ViewState的新手,那么请转储所有关于解析链接文章中提到的ViewState的guff(文章的一半)。除了高度专业化的场景之外,它根本不是必需的。做re:绝对不是正常的事:ViewState。