0

当我在代码项目中遇到一篇文章时,我正在尝试研究控制状态

http://www.codeproject.com/Articles/331981/A-Beginner-s-Tutorial-Understanding-ControlState-i

但在那个例子中,只有“文本”值保持在控制状态,如果我必须同时保持它们怎么办?所以我尝试了这段代码

    protected override void OnInit(EventArgs e)
    {
        Page.RegisterRequiresControlState(this);
        base.OnInit(e);
    }

    protected override object SaveControlState()
    {
        object[] state = new object[2]; // save the 2 properties
        state[0] = Text;
        state[1] = Text1;

        return state;
    }

    protected override void LoadControlState(object savedState)
    {
        object[] state = (object[])savedState;
        Text = (string)state[0];
        Text1 = (string)state[1];
    }

但这似乎不起作用..有人可以帮帮我吗???

提前致谢

4

1 回答 1

0

您可以使用字典或列表代替数组

protected override object SaveControlState()
{
    var state = new List<string>(); // save the 2 properties
    state.Add(Text);
    state.Add(Text1);

    return state;
}

protected override void LoadControlState(object savedState)
{
    var state = (List<string>)savedState;
    Text = state[0];
    Text1 = state[1];
}
于 2013-12-26T06:51:57.100 回答