仰天一笑

昨日不悔,今日勿失,明日莫忧! —徐羽

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  241 随笔 :: 27 文章 :: 876 评论 :: 43 引用
提交页面定位问题一直骚扰我,比如我们在DataGrid外面加了来使DataGrid在一定区域内呈现,这样如果把滚动条拉到底部的时候点击修改(或者修改后点击更新,取消)页面重新加载的时候都会把滚动条拉到页面最上面部分 ,特此我将下面知识共享一下:
object.scrollIntoView( [bAlignToTop])

Parameters

bAlignToTop Optional. Boolean that specifies one of the following values:
true Default. Scrolls the object so that top of the object is visible at the top of the window.
false Scrolls the object so that the bottom of the object is visible at the bottom of the window.

Return Value

No return value.

Remarks

The scrollIntoView method is useful for immediately showing the user the result of some action without requiring the user to manually scroll through the document to find the result.

Depending on the size of the given object and the current window, this method might not be able to put the item at the very top or very bottom, but will position the object as close to the requested position as possible.

Example

This example uses the scrollIntoView method to underline the content of the document's fifth paragraph and scroll it into view at the top of the window.

HideExample

   var coll = document.all.tags("P");
if (coll.length >= 5)
{
coll(4).style.textDecoration = "underline";
coll(4).scrollIntoView(true);
}

Standards Information

There is no public standard that applies to this method.

Applies To

INPUT type=ra...
Platform Version
Win16: 4.0
Win32: 4.0
Windows CE: 4.0
Unix: 4.0
Mac: 4.0
A, ADDRESS, APPLET, AREA, B, BIG, BLOCKQUOTE, BR, BUTTON, CAPTION, CENTER, CITE, CODE, COL, COLGROUP, COMMENT, controlRange, CUSTOM, DD, DFN, DIR, DIV, DL, DT, EM, EMBED, FIELDSET, FONT, FORM, hn, HR, I, IFRAME, IMG, INPUT type=button, INPUT type=checkbox, INPUT type=file, INPUT type=image, INPUT type=password, INPUT type=radio, INPUT type=reset, INPUT type=submit, INPUT type=text, KBD, LABEL, LEGEND, LI, LISTING, MAP, MARQUEE, MENU, NOBR, OBJECT, OL, P, PLAINTEXT, PRE, S, SAMP, SELECT, SMALL, SPAN, STRIKE, STRONG, SUB, SUP, TABLE, TBODY, TD, TEXTAREA, TextRange, TFOOT, TH, THEAD, TR, TT, U, UL, VAR, WBR, XMP
Move the mouse pointer over an element in the Applies To list to display availability information for the listed platforms.

二、在.Net中的应用
1、定位指定控件
/// <summary>
  /// 定位txtCode控件
  /// </summary>
  private void Scroll()
  {
   string s="<script>function window.onload(){document.all('"+this.txtCode.ClientID+"').scrollIntoView();}</script>";
   Page.RegisterStartupScript("",s);
  }

2、定位指定DataGrid列
 
<div style="BORDER:0px;PADDING:0px;MARGIN:0px;OVERFLOW:scroll;WIDTH:600px;HEIGHT:200px" align="center">

现在给出一种简单的办法(其他的一些利用锚点等办法都比较复杂)
private void Scroll(int index)
        
{
            
string s="<script>function window.onload(){document.all('"+this.DataGrid1.ClientID+"').rows["+index+"].scrollIntoView();}</script>";
            Page.RegisterStartupScript(
"",s);
        }

写了一个方法,其中DataGrid1换成自己的DataGrid的ID,这个方法传递进去的参数就是行号,也就是e.Item.ItemIndex。
比如在编辑操作的时候会写this.DataGrid1.EditItemIndex=e.Item.ItemIndex;
在此语句的以前加入Scroll(e.Item.ItemIndex);就可以了
同样在更新操作的时候写为Scroll(e.Item.ItemIndex);this.DataGrid1.EditItemIndex=-1;绑定;
posted on 2006-09-05 11:21 仰天一笑 阅读(3112) 评论(9)  编辑 收藏 网摘 所属分类: 原创天地ASP.Net-C#Javascript/Ajax/XML

评论

利用MaintainScrollPositionOnPostback属性
考虑到一个很长的页面,如果每次POSTBACK之后,那个IE的滚动条可能都会回到最开始的地方(最上面),而不会保留其原先已经有的位置,这样用户会比较麻烦,又要把滚动条移动到原来的地方,在asp.net 2.0中,可以设置MaintainScrollPositionOnPostback属性为TRUE即可,即
<%@ Page Language="VB" AutoEventWireup="false" MaintainScrollPositionOnPostback="true" CodeFile="MaintainScrollPosition.aspx.vb" Inherits="MaintainScrollPosition" %>

  回复  引用    

#2楼 2006-09-05 12:14 维生素C.NET      
对,我也赞成zitiger的方法,看过几个老外的程序都是这么用的。
  回复  引用  查看    

#3楼[楼主] 2006-09-05 13:25 仰天一笑      
谢谢二位,学习
  回复  引用  查看    

#4楼 2006-09-05 14:11 =兔子=[匿名]
我是用这个方法:
http://www.cnblogs.com/dboat/archive/2006/05/11/397449.html

  回复  引用    

#5楼 2006-10-24 12:36 wolf1205[未注册用户]
zitiger,维生素C.NET
你们的方式我试过了,对于page是没有任何问题的,但是对于page里面的控件就没有作用了,请问你们如果一个page里面有两个dataview都用了Scroll
,那你们该如何处理呢?

  回复  引用    

#6楼[楼主] 2006-12-18 13:48 仰天一笑      
保存页面的滚动条的位置
1/**//// <summary>
2 /// 功能:在页面post时保存屏幕滚动状态;
3 /// 直接在Page_Load中加入Zeda.CNE.Common.RetainScrollPosition();
4 /// 不能加在if(!Page.IsPostBack){}内;
5 /// 加入的两个隐藏域为__SCROLLPOS_TOP和__SCROLLPOS_LEFT
6 /// </summary>
7 public static void RetainScrollPosition()
8 {
9
10 System.Web.UI.Page page=(Page)HttpContext.Current.Handler;
11
12 page.RegisterHiddenField("__SCROLLPOS_TOP","0");
13 page.RegisterHiddenField("__SCROLLPOS_LEFT","0");
14
15 string saveScrollPosition = "<script language='javascript'>function saveScrollPosition() {{document.forms[0].__SCROLLPOS_TOP.value = {0}.scrollTop;document.forms[0].__SCROLLPOS_LEFT.value = {0}.scrollLeft ;}}{0}.onscroll=saveScrollPosition;</script>";
16 string setScrollPosition = "<script language='javascript'>function setScrollPosition() {{{0}.scrollTop ='{1}';{0}.scrollLeft ='{2}' }}{0}.onload=setScrollPosition;</script>";
17
18 page.RegisterStartupScript("saveScroll", string.Format(saveScrollPosition,"window.document.body"));
19
20 if (page.IsPostBack)
21 {
22
23 page.RegisterStartupScript("setScroll", string.Format(setScrollPosition,"window.document.body", page.Request.Form["__SCROLLPOS_TOP"],page.Request.Form["__SCROLLPOS_LEFT"]));
24 }
25 }

  回复  引用  查看    

我用vs2003怎么实现不了啊?这是作什么用的?<div style="BORDER:0px;PADDING:0px;MARGIN:0px;OVERFLOW:scroll;WIDTH:600px;HEIGHT:200px" align="center">
  回复  引用    

#8楼 2007-05-15 17:35 snake[未注册用户]
如何保持Panel的滚动条(我的Panel在页面的无刷新区域,并且该页面继承了母板页)
  回复  引用    

#9楼 2007-10-31 10:40 lzc[未注册用户]
我用了楼主的方法,不过我用的是DataGrid内部滚动。现在有这样一个问题:当我行选中一条DataGrid下边的记录后,它会自动跳到DataGrid的上边,给用户的感觉是自己选的记录不见了。其实,记录已经被选中了,只不过是位置上移了。
  回复  引用    




发表评论

昵称: [登录] [注册]

主页:

邮箱:(仅博主可见)

评论内容:

  登录  注册

[使用Ctrl+Enter键快速提交评论]

0 495027




相关文章:

相关链接: