GridView History.back()後分頁跑回第一頁

資料控制項對於返回上一頁後都有無法紀錄先前的分頁頁數的問題,本來想要放棄改用ASP的方式自己畫表格,但是這樣就浪費的ASP.NET所提供控制項的目的了,找了好久終於在網路上看到人家的解決方式,解決後又有一些小問題,在IE6.0的時候會出現一個DIV來記錄當前分頁,於是再上方出現一個空格,在這裡運用CSS來處理掉即可。

前端與法 .aspx
<style type="text/css" >
     /*====消除ScriptManager1_Navigate所產生DIV之空格======*/
     div{
      display:inline;
     }
</style>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnableHistory="True" OnNavigate="ScriptManager1_Navigate">
</asp:ScriptManager>

後端與法 .aspx.cs
//解決超連結後分頁跑回第一頁問題
private static string pageKey = "p";
protected void ScriptManager1_Navigate(object sender, HistoryEventArgs e)
{
    if (e.State.Count <= 0)
    {
        // Setup default state   
        GridView1.PageIndex = 0;
        return;
    }

    string key = e.State.AllKeys[0];
    string state = String.Empty;

    if (String.Equals(key, pageKey))
    {
        state = e.State[key];
        int pageIndex = Int32.Parse(state);
        GridView1.PageIndex = pageIndex;
        //下拉式分頁設定
        GridViewRow dgBottomPageRow;
        DropDownList drpPage;
        dgBottomPageRow = GridView1.BottomPagerRow;
        drpPage = (DropDownList)dgBottomPageRow.Cells[0].FindControl("drpPage");
        drpPage.SelectedIndex = pageIndex;
    }
}

//點選分頁時
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    if (e.NewPageIndex != -1)
    {
        GridView1.PageIndex = e.NewPageIndex;
        //紀錄回覆點
        string state = (sender as GridView).PageIndex.ToString();
        ScriptManager.GetCurrent(this).AddHistoryPoint(pageKey, state);
    }
}

//下拉式選單改變時
protected void drpPage_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow dgBottomPageRow;
    DropDownList drpPage;
    try
    {
        dgBottomPageRow = GridView1.BottomPagerRow;
        drpPage = (DropDownList)dgBottomPageRow.Cells[0].FindControl("drpPage");
        GridView1.PageIndex = Convert.ToInt32(drpPage.SelectedValue) - 1;
        //紀錄回覆點
        string state = GridView1.PageIndex.ToString();
        ScriptManager.GetCurrent(this).AddHistoryPoint(pageKey, state);
        GetData();
    }
    catch (Exception ex)
    {
        clsPublic.AJAXErrMessageBox(ex.Message, this.Page);
    }
}


參考網址 Wizard 編程網1Wizard 編程網2dotnetslackers

留言