Gridview单击编辑按钮时滚动条跳到顶部的问题
问题描述:单击Gridview编辑、选择等按钮时因引起页面提交导致DIV滚动条回到顶部,需要用户手动定位到原来位置,很不方便 解决办法: 1、没有用AJAX时,在原来<%@ Page ……后面增加MaintainScrollPositionOnPostback="true" 2、当使用了AJAX时以上方法会失效,此时需要在DIV滚动时记住滚动条位置,并在提交后将其恢复 a、为外围DIV设置ID及滚动时要执行的代码: <div id="dvGridView" style="overflow: auto; width: 200px; max-height:200px;" runat="server" onscroll="javascript:RecordPostion(this);"> b、增加2个隐藏按钮,保存值: <asp:HiddenField ID="dvscrollX" runat="server" /> <asp:HiddenField ID="dvscrollY" runat="server" /> c、脚本: <script type="text/javascript" language="javascript"> function RecordPostion(obj) { var div1 = obj; var sx = document.getElementById('dvscrollX'); var sy = document.getElementById('dvscrollY'); sy.value = div1.scrollTop; sx.value = div1.scrollLeft; } function GetResultFromServer() { try { var sx = document.getElementById('dvscrollX'); var sy = document.getElementById('dvscrollY'); document.getElementById('dvGridView').scrollTop = sy.value; document.getElementById('dvGridView').scrollLeft = sx.value; } catch (e) { } } </script> d、后台注册脚本: protected void GridView1_DataBound(object sender, EventArgs e) { string sjs = "GetResultFromServer();"; ScriptManager.RegisterClientScriptBlock(this.GridView1, this.GetType(), "", sjs, true); } |