博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

vs.Net 里页面跳转方法

Posted on 2005-12-27 16:44  欧阳另类  阅读(849)  评论(0)    收藏  举报

在页面编写的时候,经常会遇到页面过长,每次点击按钮时,页面会置顶的情况,所以写了一个小方法,可以使页面跳转,方法虽小,但很实用;
  
方法如下:

public static void BackPage(System.Web.UI.Page thePage, object sender)
{
    
string szValue = string.Format("window.location.href='#{0}'",((System.Web.UI.Control)sender).ID);
    AddEvent(thePage,
"onload",szValue);
}

在页面上还要把Body设置一下,以及调用方法

<body id="body" runat="server">//设置Body

BackPage(
this,sender);//sender也可以换成控件名称

//在上面代码中用到了AddEvent方法如下
public static void AddEvent(System.Web.UI.Page thePage, string szKey, string szValue)
{
    
string szScript = @"
        <script>
    window.document.body.
"+szKey+@"=                function()
    {
        
"+szValue+@"
    }
        </script>
       
";
thePage.RegisterClientScriptBlock(
"Js",szScript);
}
    

以上是一些小技巧,拿出来大家分享一下,有待高人指教