当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net c#在updatepanel中支持滚动条记忆功能

asp.net c#在updatepanel中支持滚动条记忆功能

2018年03月14日  | 移动技术网IT编程  | 我要评论

中华彩摘网,我是黑社会台词,狐妖小红娘2 动漫

在非ajax页面中,只要在page上设置 MaintainScrollPositionOnPostback="true" 即可进行记忆滚动条位置.

但是在ajax中,这个功能却不能正确工作了,那么我们应当如何让p自动维护滚动条位置呢?

 


首先在页面上增加 asp:panel控件 ,然后在aspx.cs后台 加入以下方法:

 


[c#]
 protected  void DoMaintainScrollPositionForAjax(UpdatePanel updatePanel,Panel panel) 
        { 
            StringBuilder sb = new StringBuilder(); 
            sb.AppendLine(@"function Panel_SaveScrollPosition(PanelID){ 
if(document.getElementById(PanelID)!=null){ 
document.getElementById(PanelID+'_ScrollPosX').value = document.getElementById(PanelID).scrollLeft; 
document.getElementById(PanelID+'_ScrollPosY').value = document.getElementById(PanelID).scrollTop;}} 
function Panel_RestoreScrollPosition(PanelID){ 
if(document.getElementById(PanelID)!=null){ 
document.getElementById(PanelID).scrollLeft = document.getElementById(PanelID+'_ScrollPosX').value; 
document.getElementById(PanelID).scrollTop = document.getElementById(PanelID+'_ScrollPosY').value;}}"); 
            ScriptManager.RegisterClientScriptBlock(updatePanel, panel.GetType(), "PanelScrollFunction", sb.ToString(), true); 
 
            HiddenField x = new HiddenField(); 
            x.ID = panel.ClientID + "_ScrollPosX"; 
            x.ClientIDMode = panel.ClientIDMode; 
           panel. Controls.Add(x); 
 
            HiddenField y = new HiddenField(); 
            y.ID = panel.ClientID + "_ScrollPosY"; 
            y.ClientIDMode = panel.ClientIDMode; 
           panel. Controls.Add(y); 
 
           string sScript = "Panel_SaveScrollPosition('" + panel.ClientID + "');"; 
           ScriptManager.RegisterOnSubmitStatement(updatePanel, panel.GetType(), panel.ID + "_SavePanelScroll", sScript); 
 
            if (Page.IsPostBack) 
            { 
                x.Value = Page.Request.Form[x.ClientID]; 
                y.Value = Page.Request.Form[y.ClientID]; 
                sScript = "Panel_RestoreScrollPosition('" + panel.ClientID + "');"; 
                ScriptManager.RegisterStartupScript(updatePanel, panel.GetType(), panel.ID + "_SetPanelScroll", sScript, true); 
            } 
        } 
 protected  void DoMaintainScrollPositionForAjax(UpdatePanel updatePanel,Panel panel)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(@"function Panel_SaveScrollPosition(PanelID){
if(document.getElementById(PanelID)!=null){
document.getElementById(PanelID+'_ScrollPosX').value = document.getElementById(PanelID).scrollLeft;
document.getElementById(PanelID+'_ScrollPosY').value = document.getElementById(PanelID).scrollTop;}}
function Panel_RestoreScrollPosition(PanelID){
if(document.getElementById(PanelID)!=null){
document.getElementById(PanelID).scrollLeft = document.getElementById(PanelID+'_ScrollPosX').value;
document.getElementById(PanelID).scrollTop = document.getElementById(PanelID+'_ScrollPosY').value;}}");
            ScriptManager.RegisterClientScriptBlock(updatePanel, panel.GetType(), "PanelScrollFunction", sb.ToString(), true);

            HiddenField x = new HiddenField();
            x.ID = panel.ClientID + "_ScrollPosX";
            x.ClientIDMode = panel.ClientIDMode;
           panel. Controls.Add(x);

            HiddenField y = new HiddenField();
            y.ID = panel.ClientID + "_ScrollPosY";
            y.ClientIDMode = panel.ClientIDMode;
           panel. Controls.Add(y);

           string sScript = "Panel_SaveScrollPosition('" + panel.ClientID + "');";
           ScriptManager.RegisterOnSubmitStatement(updatePanel, panel.GetType(), panel.ID + "_SavePanelScroll", sScript);

            if (Page.IsPostBack)
            {
                x.Value = Page.Request.Form[x.ClientID];
                y.Value = Page.Request.Form[y.ClientID];
                sScript = "Panel_RestoreScrollPosition('" + panel.ClientID + "');";
                ScriptManager.RegisterStartupScript(updatePanel, panel.GetType(), panel.ID + "_SetPanelScroll", sScript, true);
            }
        }

 

 

通过以下方法在aspx.cs调用即可


[c#]
protected void Page_Load(object sender, EventArgs e) 
        { 
           DoMaintainScrollPositionForAjax(this.UpdatePanel1, Panel1); 
        } 
protected void Page_Load(object sender, EventArgs e)
        {
           DoMaintainScrollPositionForAjax(this.UpdatePanel1, Panel1);
        }

 

 

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网