關於ViewState的使用

在寫控件的時候要用到很多的ViewState來保存狀態,例如:

public String currentPageIndex{

get{

return ViewState[“currentPageIndex“].ToString();

}

set{

ViewState[“currentPageIndex“]=value;

}

}

很多教程上使用以上的寫法,在實際應用時會出現錯誤,具體就是使用這個控件時currentPageIndex這個屬性會報錯:未將對象應用到實例!

改成這樣就可以了

public String currentPageIndex{

get{

Object obj=ViewState[“currentPageIndex“];

return (obj==null)?String.Empty:obj.ToString();

}

set{

ViewState[“currentPageIndex“]=value;

}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章