有一个页面,由于内容太长,所以出现了垂直滚动条;在页面中,有多个按钮可执行服务器端代码,如此一来,点击了某一按钮执行操作并完成操作后,页面又重新定位在顶端,如果要执行相同按钮的代码,这时候又要拖动滚动条来寻找到相应按钮才能操作,如此,给客户的印象一点都不好,有没有一种办法,能够智能的定位在页面中的位置,比如,点击哪个按钮进行操作,操作完成之后,仍然定位在那个按钮的位置,而不定位在页面顶端呢,还请大虾们赐教,小弟谢过!
---------------------------------------------------------------
首先asp.net2.0提供了简单的方法。
如果asp.net1.x,可以用@ Page SmartNavogation="true"
---------------------------------------------------------------
<%@ Page SmartNavigation="true"
---------------------------------------------------------------
http://singlepine.cnblogs.com/articles/257213.html
---------------------------------------------------------------
//cs代码
public class WebForm5 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.TextBox pos;
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Page_Load(object sender, System.EventArgs e)
{
}
protected string writebutton()
{
string script="";
script="<table>";
for(int m=0;m<100;m++)
{
script+="<tr><td>";
script+=m;
script+="</td></tr>";
}
script+="</table>";
return script;
}
}
//html代码-----------------------------
1.窗体自身滚动条定位控制
<HTML>
<HEAD>
<title>窗体自身滚动条定位控制</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<SCRIPT type="text/javascript"><!--
function onscrollform()
{
document.all["pos"].value = document.body.scrollTop;
}
function formOnload()
{
OnloadForScroll();
}
function OnloadForScroll()
{
var spos = document.all["pos"].value;
window.scrollTo( 0, spos );
}
--></SCRIPT>
</HEAD>
<BODY onscroll="onscrollform();" onload="formOnload();" scroll="yes" ms_positioning="GridLayout">
<form id="Form1" method="post" runat="server">
<%=writebutton()%>
<asp:textbox id="pos" style="DISPLAY: none" Runat="server"></asp:textbox>
<asp:Button id="Button2" style="Z-INDEX: 102; LEFT: 336px; POSITION: absolute; TOP: 520px" runat="server"
Text="可以将页面缩小,把滚动条往下拉看效果"></asp:Button>
</form>
</FORM>
</BODY>
</HTML>
2.窗体自身滚动条被禁止,而通过div的滚动条来定位控制
<HTML>
<HEAD>
<title>窗体自身滚动条被禁止,而通过div的滚动条来定位控制</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<SCRIPT type="text/javascript"><!--
function scrollDiv()
{
document.all.pos.value = document.all.onlinediv.scrollTop;//onlinediv是一个div
}
function formOnload()
{
OnloadForScroll();
}
function OnloadForScroll()
{
var spos = document.all["pos"].value;
if(! spos)spos=0;
document.all.onlinediv.scrollTop=spos;
}
--></SCRIPT>
</HEAD>
<BODY onscroll="onscrollform();" onload="formOnload();" scroll="no" ms_positioning="GridLayout">
<form id="Form1" method="post" runat="server">
<table width="300">
<tr>
<td>
<div id="onlinediv" style="OVERFLOW: auto; WIDTH: 100%; HEIGHT: 560px" onscroll="scrollDiv();">
<%=writebutton()%>
</div>
</td>
</tr>
</table>
<asp:textbox id="pos" style="DISPLAY: none" Runat="server"></asp:textbox>
<asp:Button id="Button2" style="Z-INDEX: 102; LEFT: 336px; POSITION: absolute; TOP: 520px" runat="server"
Text="可以将页面缩小,把滚动条往下拉看效果"></asp:Button>
</form>
</FORM>
</BODY>
</HTML>
------------------------------------------
单纯页面时SmartNavigation可行,但是如果页面中加载了用户控件的话,此时就不能用它了,会报错。所以可以为用text来记录滚动条的位置。