GridView的自動分頁功能是最近寫網頁常用到的功能,但是發現如果配合查詢功能對DataSource下不同的Query,分頁的下一頁因為經過了postback,會使得原來頁面的Sql Command不見了,有圖有真相
花了一點時間找出問題~~因為網頁是無狀態的環境,所以要利用ASP.NET的ViewState去記住查詢後更改的Query,怎麼做呢,就是重載SaveViewState和LoadViewState這兩個函式
protected override object SaveViewState()
{
object baseState = base.SaveViewState();
object[] myState = new object[2];
myState[0] = baseState;
myState[1] = AccessDataSource1.SelectCommand;
return myState;
}


protected override void LoadViewState(object savedState)
{
if (savedState != null)
{
// Load State from the array of objects that was saved at ;
// SavedViewState.
object[] myState = (object[])savedState;
if (myState[0] != null)
base.LoadViewState(myState[0]);
if (myState[1] != null)
AccessDataSource1.SelectCommand = (string)myState[1];
}
}

成果如下圖~
done!
Control..::.SaveViewState 方法
Control..::.LoadViewState 方法

以我為中心的宇宙 發表在 痞客邦 PIXNET 留言(0) 人氣()