How to scroll an ASP.NET control into view after page load using codebehind

该博客介绍了如何使用代码在页面加载后将ASP.NET控件滚动到可见区域,虽未给出具体内容,但指明了技术方向,对ASP.NET开发有一定参考价值。

Introduction
In some cases when an ASP.NET page loads the control you need to focus on is not visible because it is further down the page. I have had numerous occasions where a request varible indicates which item on a long list the user is interested in viewing, this script can help by scrolling the particular item into view. 

Code
The following function I have added to a Utils.dll library for gerneral use so is static and needs the current page as a variable.

public class Utils
{
public static void FocusControlOnPageLoad(string ClientID, System.Web.UI.Page page)

{

page.RegisterClientScriptBlock("CtrlFocus",

@"<script> 

function ScrollView()

{
var el = document.getElementById('"+ClientID+@"')if (el != null){el.scrollIntoView();
el.focus();}
}

window.onload += ScrollView;

</script>");

}
}

You can use this as follows:

private void Page_Load(object sender, System.EventArgs e)
{
   Utils.FocusControlOnPageLoad(this.yourcontrol.ClientID, this.Page);
}

转载于:https://www.cnblogs.com/stone/archive/2005/03/13/117890.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值