hig*_*gsy 8 c# asp.net user-controls properties arraylist
我有一个aspx页面,其中包含以下内容:
这个想法是当用户点击链接按钮时,commandarguement的值存储在List中.你可能没有问题,但我需要将值存储在usercontrol中的List中,而不是存储在ASPX页面中.列表需要在回发中保留,因此它还需要存储在视图状态中.
所以我在用户控件中创建了一个公共属性,如下所示:
public List<int> ImageString {
get {
if (this.ViewState["ImageString"] != null) {
return (List<int>)(this.ViewState["ImageString"]);
}
return new List<int>();
}
set { this.ViewState["ImageString"] = value; }
}
Run Code Online (Sandbox Code Playgroud)
然后我希望从我的aspx页面我可以添加一行代码来为列表添加一个值,如下所示:
this.LightBoxControl.ImageString.Add(value);
Run Code Online (Sandbox Code Playgroud)
我得到的问题是该值实际上从未添加到列表中.计数始终为零.
我确定它只是因为我设置了错误的属性,但我不确定如何正确...
任何帮助将不胜感激.
谢谢Al
Mic*_*ich 17
你的得主是错的.这是正确的变体:
get {
if (this.ViewState["ImageString"] == null) {
this.ViewState["ImageString"] = new List<int>();
}
return (List<int>)(this.ViewState["ImageString"]);
}
Run Code Online (Sandbox Code Playgroud)
在这里,您首先要检查ViewState中是否有您需要的东西,如果没有,您可以在那里添加它.然后从ViewState返回项目 - 保证在那里.
您的解决方案很糟糕,因为它没有new List<int>()放入ViewState
| 归档时间: |
|
| 查看次数: |
16202 次 |
| 最近记录: |