在我的代码的顶部,我有以下代码:
public class ImgData { public byte[] Image { get; set; } public string Category { get; set; } } List<ImageData> list = new List<ImageData>();我有2个Click事件
在其中一个中我有以下代码:
protected void btnAttendee_Click(object sender, EventArgs e) { ImageData data = new ImageData(); data.Category = "HR"; data.Image = imgByte1; list.Add(data); int cnt = list.Count(); // Count show 1 once I click here }在另一个地方,我有以下代码,我在上面的点击事件后触发:
protected void submit_Click(object sender, EventArgs e) { // When I try to retreive the cnt it shows 0 here. I am not deleting any items on the list so not sure why it did not retain what was in the list int cnt = list.Count(); // should have count of 1 or more }At the top of my code behind, I have the following code:
public class ImgData { public byte[] Image { get; set; } public string Category { get; set; } } List<ImageData> list = new List<ImageData>();I have 2 Click Events
In one of the them I have the following code:
protected void btnAttendee_Click(object sender, EventArgs e) { ImageData data = new ImageData(); data.Category = "HR"; data.Image = imgByte1; list.Add(data); int cnt = list.Count(); // Count show 1 once I click here }In another place, I have the following code which I fire after the click event above:
protected void submit_Click(object sender, EventArgs e) { // When I try to retreive the cnt it shows 0 here. I am not deleting any items on the list so not sure why it did not retain what was in the list int cnt = list.Count(); // should have count of 1 or more }最满意答案
Web应用程序是无状态的。
这意味着在ASP.NET的上下文中,每次向服务器发出新请求时,都会创建一个新的页面类实例 。 因此,上次填充数据的任何实例成员都将消失。 一旦将结果页面发送到客户端,就会销毁先前的实例。
为了跨页面请求保留数据,您必须将其保留在某个位置。 你有这方面的选择:
页面状态(客户端) 会话状态 一些应用程序级缓存 数据库 文件系统 等等任何持久化数据的选择都有其自身的优缺点。 因此,您需要确定最适合您需求的产品。 例如,如果您要将数据保持在会话状态,您可以这样写:
protected void btnAttendee_Click(object sender, EventArgs e) { ImageData data = new ImageData(); data.Category = "HR"; data.Image = imgByte1; list.Add(data); int cnt = list.Count(); // Count show 1 once I click here Session["myList"] = list; }然后在这里检索它:
protected void submit_Click(object sender, EventArgs e) { List<ImageData> list = (List<ImageData>)Session["myList"]; int cnt = list.Count(); // should have count of 1 or more }鉴于此,如果成员不再需要是实例级别,您甚至可以重新修改一下您的类。 它可能更好,只是一个方法级别的变量。
Web applications are stateless.
What this means in the context of ASP.NET is that each time you make a new request to the server, a new instance of your page class is created. So any instance members you populated with data last time are gone. That previous instance was destroyed as soon as the resulting page was sent to the client.
In order to retain data across page requests, you have to persist it somewhere. You have options in that regard:
Page state (client-side) Session state Some application-level cache Database File system etc.Any option for persisting data is going to have its own pros and cons. So it's really up to you to determine what's best for your needs. As an example, if you were to persist the data in session state, you might write it like this:
protected void btnAttendee_Click(object sender, EventArgs e) { ImageData data = new ImageData(); data.Category = "HR"; data.Image = imgByte1; list.Add(data); int cnt = list.Count(); // Count show 1 once I click here Session["myList"] = list; }Then retrieve it here:
protected void submit_Click(object sender, EventArgs e) { List<ImageData> list = (List<ImageData>)Session["myList"]; int cnt = list.Count(); // should have count of 1 or more }Given this, you may even re-strcuture your class a bit if the member no longer needs to be instance-level. It might be better as simply a method-level variable.
列出Click事件之间丢失的项目(List items lost between Click Events)在我的代码的顶部,我有以下代码:
public class ImgData { public byte[] Image { get; set; } public string Category { get; set; } } List<ImageData> list = new List<ImageData>();我有2个Click事件
在其中一个中我有以下代码:
protected void btnAttendee_Click(object sender, EventArgs e) { ImageData data = new ImageData(); data.Category = "HR"; data.Image = imgByte1; list.Add(data); int cnt = list.Count(); // Count show 1 once I click here }在另一个地方,我有以下代码,我在上面的点击事件后触发:
protected void submit_Click(object sender, EventArgs e) { // When I try to retreive the cnt it shows 0 here. I am not deleting any items on the list so not sure why it did not retain what was in the list int cnt = list.Count(); // should have count of 1 or more }At the top of my code behind, I have the following code:
public class ImgData { public byte[] Image { get; set; } public string Category { get; set; } } List<ImageData> list = new List<ImageData>();I have 2 Click Events
In one of the them I have the following code:
protected void btnAttendee_Click(object sender, EventArgs e) { ImageData data = new ImageData(); data.Category = "HR"; data.Image = imgByte1; list.Add(data); int cnt = list.Count(); // Count show 1 once I click here }In another place, I have the following code which I fire after the click event above:
protected void submit_Click(object sender, EventArgs e) { // When I try to retreive the cnt it shows 0 here. I am not deleting any items on the list so not sure why it did not retain what was in the list int cnt = list.Count(); // should have count of 1 or more }最满意答案
Web应用程序是无状态的。
这意味着在ASP.NET的上下文中,每次向服务器发出新请求时,都会创建一个新的页面类实例 。 因此,上次填充数据的任何实例成员都将消失。 一旦将结果页面发送到客户端,就会销毁先前的实例。
为了跨页面请求保留数据,您必须将其保留在某个位置。 你有这方面的选择:
页面状态(客户端) 会话状态 一些应用程序级缓存 数据库 文件系统 等等任何持久化数据的选择都有其自身的优缺点。 因此,您需要确定最适合您需求的产品。 例如,如果您要将数据保持在会话状态,您可以这样写:
protected void btnAttendee_Click(object sender, EventArgs e) { ImageData data = new ImageData(); data.Category = "HR"; data.Image = imgByte1; list.Add(data); int cnt = list.Count(); // Count show 1 once I click here Session["myList"] = list; }然后在这里检索它:
protected void submit_Click(object sender, EventArgs e) { List<ImageData> list = (List<ImageData>)Session["myList"]; int cnt = list.Count(); // should have count of 1 or more }鉴于此,如果成员不再需要是实例级别,您甚至可以重新修改一下您的类。 它可能更好,只是一个方法级别的变量。
Web applications are stateless.
What this means in the context of ASP.NET is that each time you make a new request to the server, a new instance of your page class is created. So any instance members you populated with data last time are gone. That previous instance was destroyed as soon as the resulting page was sent to the client.
In order to retain data across page requests, you have to persist it somewhere. You have options in that regard:
Page state (client-side) Session state Some application-level cache Database File system etc.Any option for persisting data is going to have its own pros and cons. So it's really up to you to determine what's best for your needs. As an example, if you were to persist the data in session state, you might write it like this:
protected void btnAttendee_Click(object sender, EventArgs e) { ImageData data = new ImageData(); data.Category = "HR"; data.Image = imgByte1; list.Add(data); int cnt = list.Count(); // Count show 1 once I click here Session["myList"] = list; }Then retrieve it here:
protected void submit_Click(object sender, EventArgs e) { List<ImageData> list = (List<ImageData>)Session["myList"]; int cnt = list.Count(); // should have count of 1 or more }Given this, you may even re-strcuture your class a bit if the member no longer needs to be instance-level. It might be better as simply a method-level variable.
发布评论