Search...

Wednesday, February 8, 2012

Mantain scroll on postback


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Mantain scroll on postback</title>
    <script src="jquery-1.3.2.min.js" type="text/javascript"></script>
    <!--Downloads available in http://jqueryui.com/ -->
</head>
<body>
    <form id="form1" runat="server">
    <asp:Button ID="Button1" runat="server" Text="Click Me"
        onclick="Button1_Click" />
    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
    <p style="width:100%;text-align:center">Mantain scroll on postback</p>
    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
    <asp:Button ID="Button2" runat="server" Text="Mantain Scroll"
        onclick="Button2_Click" /><span style="margin:0;border:0;padding:0" id="mantainFocus" />
    </form>
</body>
</html>
<script type="text/javascript">
function show(){
alert('Hello World !');
}
</script>

********************************************************************************


using System;
using System.Web.UI;

public partial class _Default : System.Web.UI.Page
{
   

    protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Attributes.Add("onclick", "show()");
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        String jsName = "MyScript";
        Type jsType = this.Page.GetType();
        ClientScriptManager csm = this.Page.ClientScript;
        if (!csm.IsStartupScriptRegistered(jsType, jsName))
        {
            String jsText = "show();";
            csm.RegisterStartupScript(jsType, jsName, jsText, true);
        }  
       
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        String jsName = "MantainFocus";
        Type jsType = this.Page.GetType();
        ClientScriptManager csm = this.Page.ClientScript;
        String jsText = "$(window).scrollTop($('#mantainFocus').offset().top);";
        csm.RegisterStartupScript(jsType, jsName, jsText, true);
   
    }
   
}

No comments: