asp.net c#在updatepanel中支持滚动条记忆功能
2013-06-26 16:31
405 查看
在非ajax页面中,只要在page上设置 MaintainScrollPositionOnPostback="true" 即可进行记忆滚动条位置.
但是在ajax中,这个功能却不能正确工作了,那么我们应当如何让div自动维护滚动条位置呢?
首先在页面上增加 asp:panel控件 ,然后在aspx.cs后台 加入以下方法:
通过以下方法在aspx.cs调用即可
但是在ajax中,这个功能却不能正确工作了,那么我们应当如何让div自动维护滚动条位置呢?
首先在页面上增加 asp:panel控件 ,然后在aspx.cs后台 加入以下方法:
/// /// 记住panel的滚动条位置 /// /// panel控件实例 /// ajax更新区域,可以为null,如果为null则执行非ajax逻辑,非ajax逻辑建议直接设置page的MaintainScrollPositionOnPostBack属性 protected void DoMaintainScrollPosition(Panel panel,UpdatePanel updatePanel=null) { 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;}}"); 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 + "');"; if (updatePanel == null) { Page.ClientScript.RegisterClientScriptBlock(panel.GetType(), "PanelScrollFunction", sb.ToString(), true); Page.ClientScript.RegisterOnSubmitStatement(panel.GetType(), panel.ID + "_SavePanelScroll", sScript); } else { ScriptManager.RegisterClientScriptBlock(updatePanel, panel.GetType(), "PanelScrollFunction", sb.ToString(), true); 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 + "');"; if (updatePanel == null) { Page.ClientScript.RegisterStartupScript(panel.GetType(), panel.ID + "_SetPanelScroll", sScript, true); } else { ScriptManager.RegisterStartupScript(updatePanel, panel.GetType(), panel.ID + "_SetPanelScroll", sScript, true); } } }
通过以下方法在aspx.cs调用即可
protected void Page_Load(object sender, EventArgs e) { DoMaintainScrollPositionForAjax(Panel1,UpdatePanel1); }
相关文章推荐
- 晚上无聊象征性收取了网友100元辛苦费,实现支持多语言功能的XML语言包版的C#的ASP.NET多语言支持例子程序
- 晚上无聊象征性收取了网友100元辛苦费,实现支持多语言功能的XML语言包版的C#的ASP.NET多语言支持例子程序
- 晚上无聊象征性收取了网友100元辛苦费,实现支持多语言功能的XML语言包版的C#的ASP.NET多语言支持例子程序
- 晚上无聊象征性收取了网友100元辛苦费,实现支持多语言功能的XML语言包版的C#的ASP.NET多语言支持例子程序
- 晚上无聊象征性收取了网友100元辛苦费,实现支持多语言功能的XML语言包版的C#的ASP.NET多语言支持例子程序
- 晚上无聊象征性收取了网友100元辛苦费,实现支持多语言功能的XML语言包版的C#的ASP.NET多语言支持例子程序
- 晚上无聊象征性收取了网友100元辛苦费,实现支持多语言功能的XML语言包版的C#的ASP.NET多语言支持例子程序
- 晚上无聊象征性收取了网友100元辛苦费,实现支持多语言功能的XML语言包版的C#的ASP.NET多语言支持例子程序
- 晚上无聊象征性收取了网友100元辛苦费,实现支持多语言功能的XML语言包版的C#的ASP.NET多语言支持例子程序
- 晚上无聊象征性收取了网友100元辛苦费,实现支持多语言功能的XML语言包版的C#的ASP.NET多语言支持例子程序
- 晚上无聊象征性收取了网友100元辛苦费,实现支持多语言功能的XML语言包版的C#的ASP.NET多语言支持例子程序
- asp.net生成高质量缩略图通用函数(c#代码),支持多种生成方式
- ASP.NET C#开发Excel导入的功能流程
- Asp.net(C#)实现验证码功能
- 【商业版】C# ASP.NET 通用权限管理系统组件源码中的数据库访问组件可以全面支持Access单机数据库了
- Asp.net(C#)实现验证码功能
- 让ASP.NET的FileUpload控件支持浏览自动上传功能
- Asp.net(C#)实现验证码功能代码
- asp.net生成高质量缩略图通用函数(c#代码),支持多种生成方式
- Asp.net(C#)实现验证码功能