Asp.net 获取和设置不使用列表<&燃气轮机;
我正试着做一个get设置,让我的列表持久化。我想我的意图是明确的。我希望列表在回发过程中保持不变,这样我就不会丢失其中的数据 我做错了什么?多亏了我的AlertPopUp,我可以看到Get被触发,但从来没有设置过。因此,当列表应该包含多个项时,它返回为containint zero itemsAsp.net 获取和设置不使用列表<&燃气轮机;,asp.net,list,properties,Asp.net,List,Properties,我正试着做一个get设置,让我的列表持久化。我想我的意图是明确的。我希望列表在回发过程中保持不变,这样我就不会丢失其中的数据 我做错了什么?多亏了我的AlertPopUp,我可以看到Get被触发,但从来没有设置过。因此,当列表应该包含多个项时,它返回为containint zero items private List<string> accountIDsSelectedForDeletion = new List<string>(); public List<s
private List<string> accountIDsSelectedForDeletion = new List<string>();
public List<string> AccountIDsSelectedForDeletion
{
get {
if (ViewState["AccountIDsSelectedForDeletion"]!= null)
{
accountIDsSelectedForDeletion = ViewState["AccountIDsSelectedForDeletion"] as List<string>;
AlertPopUp.QuickDebugMessage("getting list from viewstate. Count: "+ accountIDsSelectedForDeletion.Count);
}
AlertPopUp.QuickDebugMessage("returning list");
return accountIDsSelectedForDeletion;
}
set {
AlertPopUp.QuickDebugMessage("setting list to viewstate. Count: " + accountIDsSelectedForDeletion.Count);
accountIDsSelectedForDeletion = value;
ViewState["AccountIDsSelectedForDeletion"] = accountIDsSelectedForDeletion;
}
private List accountidssectedfordeletion=new List();
公共列表帐户已选择删除
{
得到{
如果(ViewState[“AccountIDSelectedForDelete”]!=null)
{
AccountIDSelectedForDelete=ViewState[“AccountIDSelectedForDelete”]作为列表;
AlertPopUp.QuickDebugMessage(“从viewstate.Count获取列表:+AccountIDSelectedForDelete.Count”);
}
AlertPopUp.QuickDebugMessage(“返回列表”);
选择用于删除的返回帐户ID;
}
设置{
AlertPopUp.QuickDebugMessage(“将列表设置为viewstate.Count:+AccountIDSelectedForDelete.Count”);
AccountIDSelectedForDelete=值;
视图状态[“AccountIDSelectedForDelete”]=AccountIDSelectedForDelete;
}
可能是因为我通过Add()方法更改了列表吗 简而言之,是的。
.Add()
正在改变列表,而Set属性作为一个集合在列表上起作用
为了完成您在此处尝试执行的操作,我将从列表中创建一个继承类型,以便将代码注入添加删除(ext,还有更多)方法。例如:
//not syntatically correct sorry
public class MyList : List<string>
{
public override Add(string NewItem)
{
//do a extra peristance step here
//
me.Add(NewItem);
}
}
//语法不正确抱歉
公共类MyList:列表
{
公共覆盖添加(字符串NewItem)
{
//在这里多走一步
//
me.Add(NewItem);
}
}
或者使用一个类包装私有列表并管理添加/删除方法,可能类似于:
public class MyList
{
private List<string> listItems { get; set; }
//don't expose the list in a mutable state, IEnumerable instead
public IEnumerable<string> Items { get { return this.listItems; } }
public void Add(string NewItem)
{
this.ListItems.Add(NewItem);
}
}
公共类MyList
{
私有列表列表项{get;set;}
//不要在可变状态下公开列表,而是IEnumerable
公共IEnumerable项{get{返回this.listItems;}}
公共无效添加(字符串NewItem)
{
this.ListItems.Add(NewItem);
}
}
然后在页面级类中用这种类型替换列表
顺便说一句,坚持使用ViewState
会增加每页加载时必须上下发送的信息量。您是否考虑过Session
,或者Cache
对象?可能是因为我通过Add()更改了列表方法?何时以及如何将项目添加到此列表?在getter中执行双重查找。使用var list=ViewState[“”]作为列表;if(list!=null)
。