AXzhz

专注ASP.NET!
        写软件的都是小姐,用软件的都是大爷。真TMD说的正确!
                嫖妓不给钱,反说被强奸!___中国共享软件的无奈!
读小学的时候大学不要钱,读大学了小学不要钱。
没工作时国家有分福利房,工作了后全是高价房!!

                        此软件能有效的破解QQ密码。(注:破解QQ密码是违法行为,请不要将软件用于违法行为)
“安得广厦千万间,大庇天下寒士俱欢颜,风雨不动安如山。”1200多年前,诗人杜甫的理想,如今被一帮享受着电脑和互联网带来的快捷生活方式的受过高等教育的大孩子憧憬着。

导航

页面回传(PostBack)后恢复滚动条的位置_AX

【总结】
其中涉及到了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);
    }

}

posted on 2008-07-17 18:14  斧头帮少帮主  阅读(1116)  评论(7编辑  收藏  举报

Google
 
站内搜索:        
园内搜索:
金山词霸: