頁面回傳(PostBack)後恢複捲軸的位置_AX

來源:互聯網
上載者:User
【總結】
其中涉及到了DTD模板標準,比較煩人,還得判斷下,只在IE瀏覽器測試通過.
就是這個,VS2008頁面都有這個了,會影響樣式和指令碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

在Page_Load裡直接調用RestoreScroll方法就OK了,詳見代碼注釋.
【代碼】using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/**//// <summary>
///AX 的摘要說明
/// </summary>
public class AX
{
    public AX()
    {
    }

    public static void RestoreScroll(Page page)
    {
        //註冊一個Hidden Filed
        page.ClientScript.RegisterHiddenField("Hidden_AX", "0");
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        //當捲軸滾動時將其到頁頂的位置儲存在Hidden Field中
        sb.AppendLine("function getPageScroll()                                         ");
        sb.AppendLine("{                                                                ");
        sb.AppendLine("    var yScroll;                                                 ");
        sb.AppendLine("    if (document.documentElement.scrollTop)//符合DTD標準的Page   ");
        sb.AppendLine("    {                                                            ");
        sb.AppendLine("        yScroll = document.documentElement.scrollTop;            ");
        sb.AppendLine("    }                                                            ");
        sb.AppendLine("    else                                                         ");
        sb.AppendLine("    {                                                            ");
        sb.AppendLine("        yScroll = document.body.scrollTop;                       ");
        sb.AppendLine("    }                                                            ");
        sb.AppendLine("    return yScroll;                                              ");
        sb.AppendLine("}                                                                ");
        sb.AppendLine("function saveScrollPosition()                                    ");
        sb.AppendLine("{                                                                ");
        sb.AppendLine("    document.getElementById('Hidden_AX').value = getPageScroll();");
        sb.AppendLine("}                                                                ");
        sb.AppendLine("window.onscroll=saveScrollPosition;                              ");
        page.ClientScript.RegisterStartupScript(page.GetType(), "AX", sb.ToString(), true);

        //頁面載入完後恢複捲軸的位置
        if (page.IsPostBack)
        {
            sb.Remove(0,sb.Length);
            sb.AppendLine("function setScrollPosition()                                      ");
            sb.AppendLine("{                                                                 ");
            sb.AppendLine("    window.scrollTo(0," + page.Request["Hidden_AX"] + ");         ");
            sb.AppendLine("}                                                                 ");
            sb.AppendLine("document.body.onload=setScrollPosition;                           ");
            page.ClientScript.RegisterStartupScript(page.GetType(), "AXzhz", sb.ToString(), true);
        }
    }
}

調用public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        AX.RestoreScroll(Page);
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.