开发者

asp:linkbutton (navigating to specific page section on post back)

I have some linkbuttons to update my gridview which is in the middle of the page开发者_运维百科. Everytime I hit edit or delete etc the window scrolls to the top on the page that gets posted back. I want it to stay focused on the grideview. I have tried a javascript function but for some reason it did not work.

(edit: the following works as far as scrolling is concerned but prevents postback)

here is what I tried

<script type="text/javascript" language="javascript">  

    function goto()   {   

         window.scrollTo(10, 1100);   

    }   

</script>

<asp:LinkButton ID="lbtnGo" runat="server"   OnClientClick="javascript:goto();return false;">GO</asp:LinkButton> 

source

How can I do this?


Did you try with <%@ Page MaintainScrollPositionOnPostback="true" %> in the page declaration?

Regards


Client-side event fires before server-side. So even if you scroll window to correct position - after postback you will be returned to the top. You can add the following code to your server-side LinkButton click event handler:

  if (!this.IsStartupScriptRegistered("ScrollToGrid"))
    {
      String scriptString = "<script language=\"JavaScript\">";
      scriptString += "window.scrollTo(10, 1100);";
      scriptString += "</script>";
      this.RegisterStartupScript("ScrollToGrid", scriptString);
    }

this will add javascript block to your page after postback


There are, depending on the .NET framework properties available that can help one out:

ASP.NET 1.x: use SmartNavigation. ASP.NET 2.0: use MaintainScrollPositionOnPostBack. Use an UpdatePanel control to asynchronously update parts of a page

and the best way for this is UpdatePanel control to asynchronously update parts of a page

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜