[英]C# Adding item into List<> doesn't increase its size
I'm playing a little bit in ASP.NET and here is some code to display list size and some buttons to manipulate it. 我在ASP.NET中玩了一点,这是一些用于显示列表大小的代码,以及一些用于操作列表的按钮。
I have separated class to create the list with a constructor. 我已经分离了类以使用构造函数创建列表。 Here is also a method to increase its size 这也是增加其大小的方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public class Class1
{
public List<byte> Pole { get; set; }
public int velikost_pole { get; set; } //list size
public Class1() //constructor, default size is 3
{
List<byte> pole = new List<byte>();
for (byte a = 0; a < 3; a++)
{
pole.Add((byte)(a * a));
}
Pole = pole;
velikost_pole = Pole.Count;
}
public string zvetsit_pole() //method to increase the list size by adding item
{
if (Pole.Count < 6) //max size
{
Pole.Add((byte)(Pole.Count*Pole.Count+1));
velikost_pole = Pole.Count;
return "Pole zvetseno o 1";
}
else
{
return "Jiz nelze zvetsit";
}
}
}
In the main file, the object is created as global variable and there is a button to increase the list size. 在主文件中,对象创建为全局变量,并且有一个用于增加列表大小的按钮。 However, the size doesnt change and its still the same 但是,大小不变,并且仍然相同
public partial class _Default : System.Web.UI.Page
{
Class1 objekt = new Class1(); //create the object
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Velikost pole je: " + objekt.velikost_pole.ToString() + "<br />";
for (int a=0;a<objekt.velikost_pole;a++)
{
Label1.Text += objekt.Pole[a].ToString() + " ";
}
}
protected void Button2_Click(object sender, EventArgs e) //call method to increase the size
{
Label1.Text = objekt.zvetsit_pole();
}
} }
Thanks for ideas. 感谢您的想法。
Remember that ASP.NET is stateless so each request creates a completely new Page
object. 请记住,ASP.NET是无状态的,因此每个请求都会创建一个全新的Page
对象。 So your activity timeline looks like this: 因此,您的活动时间表如下所示:
Page
created Page
创建 objekt
created objekt
创建 Page
created Page
创建 objekt
created ( not the same object as the first reqest) objekt
创建( 不是同一个对象作为第一传请求) Label1.Text
set Label1.Text
集 Page
created Page
创建 objekt
created ( not the same object as either of the first two reqests) objekt
创建(与前两个请求中的任何一个都不相同) zvetsit_pole
called zvetsit_pole
称为 Label1.Text
set to return value Label1.Text
设置为返回值 So the button click events are dealing with different objects, so you don't see the results of one action in the next action. 因此,按钮单击事件处理的是不同的对象,因此在下一个动作中看不到一个动作的结果。
The answer is to put objekt
in some sort of persistent storage like ViewState
, Cache
, or Session
. 答案是把objekt
一些有点像永久存储ViewState
, Cache
或Session
。 If you don't know which is appropriate for your needs do some research and decide for yourself. 如果您不知道哪一种适合您的需求,请进行一些研究并自己决定。
Each request creates a new instance of the _Default
class, and correspondingly, objekt
will be a new instance of Class1
as well. 每个请求创建一个新实例_Default
类,相应地, objekt
将是一个新的实例Class1
为好。
This means that any change you make to objekt
in one request will not be reflected in the next request. 这意味着你做任何改变objekt
在一个请求都不会在下次请求中得到反映。
In order for the changes to be persisted between requests you will need to store them somewhere. 为了使更改在请求之间得以保留,您需要将其存储在某个位置。 There are different options to do that, some involve using the user's session, others using the page's view state or maybe persisting the state to disk. 有多种选择可以做到这一点,有些涉及使用用户的会话,另一些涉及使用页面的视图状态,或者可能将状态持久化到磁盘上。 Which one you choose depends on what exactly you want to accomplish. 您选择哪一个取决于您究竟要完成什么。
For additional info, see pages like these: 有关其他信息,请参见以下页面:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.