幾個Postback時常用的javascript方法

來源:互聯網
上載者:User

有時候選擇下拉框自動postback時,由於頁面太長,postback後頁面自動滾動到最上面,不能保持postback之前的位置,如果在Page_Load調用下面的方法就可以解決這個問題:

/**//// <summary> 
/// Persist ScrollBar's Position After a PostBack 
/// </summary> 
private void PersistScrollPosition() 
        { 
            System.Text.StringBuilder saveScrollPosition = new System.Text.StringBuilder(); 
            System.Text.StringBuilder setScrollPosition = new System.Text.StringBuilder(); 

            Page.RegisterHiddenField("__SCROLLPOS", "0"); 

            saveScrollPosition.Append("<script language='javascript'>"); 
            saveScrollPosition.Append("function saveScrollPosition() {"); 
            saveScrollPosition.Append(" document.forms[0].__SCROLLPOS.value = document.body.scrollTop;"); 
            saveScrollPosition.Append("}"); 
            saveScrollPosition.Append("document.body.onscroll=saveScrollPosition;"); 
            saveScrollPosition.Append("</script>"); 

            Page.RegisterStartupScript("saveScroll", saveScrollPosition.ToString()); 

            if (Page.IsPostBack) 
            { 
                setScrollPosition.Append("<script language='javascript'>"); 
                setScrollPosition.Append("function setScrollPosition() {"); 
                setScrollPosition.Append(" document.body.scrollTop = " + Request["__SCROLLPOS"] + ";"); 
                setScrollPosition.Append("}"); 
                setScrollPosition.Append("document.body.onload=setScrollPosition;"); 
                setScrollPosition.Append("</script>"); 

                Page.RegisterStartupScript("setScroll", setScrollPosition.ToString()); 
            } 
        }

跳到指定位置:public void MoveTo(string name)
        {
            System.Text.StringBuilder sbScript = new System.Text.StringBuilder();

            sbScript.Append("<script language='JavaScript' type='text/javascript'>\n");
            sbScript.Append("<!--\n");
            sbScript.Append("document.location = '" + name + "';\n");
            sbScript.Append("// -->\n");
            sbScript.Append("</script>\n");

            Page.RegisterClientScriptBlock("MoveToNext", sbScript.ToString());
        }

 設定焦點到指定控制項:

public static void SetFocus(System.Web.UI.Page Page,System.Web.UI.Control control)
        {
            StringBuilder sb = new StringBuilder();
    
            sb.Append("\r\n<script language='JavaScript'>\r\n");
            sb.Append("<!--\r\n"); 
            sb.Append("function SetFocus()\r\n"); 
            sb.Append("{\r\n"); 
            sb.Append("\tdocument.");
    
            System.Web.UI.Control p = control.Parent;
            while (!(p is System.Web.UI.HtmlControls.HtmlForm)) p = p.Parent; 
    
            sb.Append(p.ClientID);
            sb.Append("['"); 
            sb.Append(control.UniqueID); 
            sb.Append("'].focus();\r\n"); 
            sb.Append("}\r\n"); 
            sb.Append("window.onload = SetFocus;\r\n"); 
            sb.Append("// -->\r\n"); 
            sb.Append("</script>");
    
            control.Page.RegisterClientScriptBlock("SetFocus", sb.ToString());
        }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.