vijay

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


Introduction
Here I share the code to keep the scroll position in the panel or Div.
Description:
Normally we use scroll position in Div or panel…when the size goes large..at when doing postback for submitting any button the scroll positions would go to top…..so 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     
       </asp:Panel>
    </div>
Server side code:
protected void Page_Load(object sender, EventArgs e)
    {
        ScrolBar();
    }

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

        PosX.ID = "PosX";
        PosY.ID = "PosY";
        form1.Controls.Add(PosX);
        form1.Controls.Add(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……….

0 comments:

Post a Comment