ASP.net使用C#保持在回发一个列表 [英] ASP.net with C# Keeping a List on postback
本文介绍了ASP.net使用C#保持在回发一个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的情况是这样的:我有这些列表插入到他们,当用户按下添加按钮的数据,但我想在回发的列表被重新归零。你如何让他们保留下来?我一直在寻找的答案,但我想我不太了解如何使用会话等。
我很新的ASP.net ,并没有多少用C#更好它似乎
公共部分类主营:System.Web.UI.Page
{
名单,LT;字符串>代码=新的List<串GT;();
保护无效的Page_Load(对象发件人,EventArgs五)
{
//的Bleh
}
保护无效cmdAdd_Click(对象发件人,EventArgs五)
{
code.Add(lstCode.Text);
}
解决方案
只要使用这个属性来存储信息:
公开名单<串GT;代码
{
得到
{
如果(HttpContext.Current.Session [密码] == NULL)
{
HttpContext.Current.Session [密码] =新的List<串GT;();
}
返回HttpContext.Current.Session [密码]作为名单<字符串取代;
}
组
{
HttpContext.Current.Session [编号] =值;
}
}
My situation goes like this: I have these lists with data inserted into them when a user presses an ADD button, but I guess on postback the Lists are re-zeroed. How do you keep them preserved? I've been looking for the answer, but I guess I don't quite understand how to use the session, etc.
I'm very new to ASP.net and not much better with C# it would seem.
public partial class Main : System.Web.UI.Page
{
List<string> code = new List<string>();
protected void Page_Load(object sender, EventArgs e)
{
//bleh
}
protected void cmdAdd_Click(object sender, EventArgs e)
{
code.Add(lstCode.Text);
}
解决方案
Just use this property to store information:
public List<string> Code
{
get
{
if(HttpContext.Current.Session["Code"] == null)
{
HttpContext.Current.Session["Code"] = new List<string>();
}
return HttpContext.Current.Session["Code"] as List<string>;
}
set
{
HttpContext.Current.Session["Code"] = value;
}
}
这篇关于ASP.net使用C#保持在回发一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文