如何在aspx中设置控件状态

时间:2013-12-26 06:15:11

标签: c# control-state

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

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

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

    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];
    }

但它似乎没有用..有谁能帮帮我???

提前致谢

1 个答案:

答案 0 :(得分: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];
}
相关问题