透過SqlDataSource查詢後GridView排序或是分頁查詢就不見了

摘要:透過SqlDataSource查詢後GridView排序或是分頁查詢就不見了

掯...很無理頭的問題

為啥會不見勒

這不是常常要用到的功能

查了查 原來是ViewState無法記憶得手動加入兩段程式碼就好了

/// <summary>
    /// 由 ViewState 還原控制項的狀態。
    /// </summary>
    /// <param name="savedState">要還原的控制項狀態。</param>

    protected override void LoadViewState(object savedState)
    {
        if (savedState != null)
        {
            object[] myState = (object[])savedState;
            if (myState[0] != null)
            {
                base.LoadViewState(myState[0]);
            }

            if (myState[1] != null)
            {
                SqlDataSource1.SelectCommand = Convert.ToString(myState[1]);
            }

        }

    }



    /// <summary>
    /// 控制項的狀態儲存至 ViewState
    /// </summary>
    /// <returns>含有控制項之目前檢視狀態的物件。</returns>

    protected override object SaveViewState()
    {
        object baseSate = base.SaveViewState();
        object[] myState = new object[2];
        myState[0] = baseSate;
        myState[1] = SqlDataSource1.SelectCommand;
        return myState;
    }