welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Tuesday, January 10, 2012

Maintain scroll position in panel or maintain scroll position when postback in ASP.Net

Here I share the code to keep the scroll position in the panel or Div.
Normally we use scroll position in Div or panel…when the size goes when doing postback for submitting any button the scroll positions would go to top… for maintaining the scroll position we have to write the piece of code to get the scroll position and again we give the position to panel scroll….
I have got this type of problem...then I googled then lastly I found this code..
<div >
        <asp:Panel ID="Panel1" runat="server" ScrollBars ="Both" Height="100">
Here Gridview or your design     
Server side code:
protected void Page_Load(object sender, EventArgs e)

    private void ScrolBar()
        HiddenField PosX = new HiddenField();
       HiddenField PosY = new HiddenField();

        PosX.ID = "PosX";
        PosY.ID = "PosY";

        string script;
        script = "window.document.getElementById('" + PosX.ClientID + "').value = "
                  + "window.document.getElementById('" + Panel1.ClientID + "').scrollLeft;"
                  + "window.document.getElementById('" + PosY.ClientID + "').value = "
                  + "window.document.getElementById('" + Panel1.ClientID + "').scrollTop;";

        this.ClientScript.RegisterOnSubmitStatement(this.GetType(), "SavePanelScroll", script);

        if (IsPostBack)
            script = "window.document.getElementById('" + Panel1.ClientID + "').scrollLeft = "
                    + "window.document.getElementById('" + PosX.ClientID + "').value;"
                    + "window.document.getElementById('" + Panel1.ClientID + "').scrollTop = "
                    + "window.document.getElementById('" + PosY.ClientID + "').value;";

            this.ClientScript.RegisterStartupScript(this.GetType(), "SetPanelScroll", script, true);

I hope this one would helpful to someone……….


Post a Comment