Solved

Maintain scrolling in Panel after postback

Posted on 2008-06-18
10
4,131 Views
Last Modified: 2012-05-05
I have Panel with some control in it. At the very bottom of panel, I have checkbox which has Autopost=True.
When I click on checkbox, Panel takes me to first control at the top in the panel. It does not maintain its position.
<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        &nbsp;

        <asp:ScriptManager ID="ScriptManager1" runat="server">

        </asp:ScriptManager>

    </div>

        <asp:UpdatePanel ID="UpdatePanel1" runat="server">

            <ContentTemplate>

                <asp:Panel ID="Panel1" runat="server" Height="184px" ScrollBars="Vertical" Style="z-index: 100;

                            left: 0px; position: absolute; top: 0px" Width="376px">

                            <asp:TextBox ID="TextBox1" runat="server" Style="z-index: 100; left: 16px; position: absolute;

                                top: 24px"></asp:TextBox>

                            <asp:TextBox ID="TextBox2" runat="server" Style="z-index: 101; left: 16px; position: absolute;

                                top: 64px"></asp:TextBox>

                            <asp:TextBox ID="TextBox3" runat="server" Style="z-index: 102; left: 16px; position: absolute;

                                top: 104px"></asp:TextBox>

                            <asp:TextBox ID="TextBox4" runat="server" Style="z-index: 103; left: 16px; position: absolute;

                                top: 144px"></asp:TextBox>

                            <asp:TextBox ID="TextBox5" runat="server" Style="z-index: 104; left: 120px; position: absolute;

                                top: 280px"></asp:TextBox>

                            <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" Style="z-index: 106;

                                left: 8px; position: absolute; top: 280px" />

                        </asp:Panel>

            </ContentTemplate>

        </asp:UpdatePanel>

    </form>

</body>

</html>

Open in new window

0
Comment
Question by:siddiqishahid
  • 6
  • 4
10 Comments
 
LVL 16

Expert Comment

by:burakiewicz
Comment Utility
you can store the position of the panel in a hiddenfield and on the page load reset the position with javascript
0
 

Author Comment

by:siddiqishahid
Comment Utility
Thanks for your reply. Can you give me sample code?

Thanks.
0
 
LVL 16

Expert Comment

by:burakiewicz
Comment Utility
javascript is first,  on your panel add onscroll="SaveScrollPosition();"

this is c# code but should be close to the same in VB
put this in your pageload, the datetime now is added so the id will be unique
ClientScript.RegisterStartupScript(this.GetType(), "RestorePosition" + DateTime.Now.Ticks, "RestoreScrollPosition()", true);
           
put this in the page or a sepereate js file

<script type="text/javascript">

    function SaveScrollPosition()

    {

        pnl=document.getElementById('Panel1')

        hdn=document.getElementById('hdnScrollPosition')

        hdn.value=pnl.scrollTop;

    }

    function RestoreScrollPosition()

    {

        pnl=document.getElementById('Panel1')

        hdn=document.getElementById('hdnScrollPosition')

        pnl.scrollTop=hdn.value;

    }

    </script>

Open in new window

0
 
LVL 16

Expert Comment

by:burakiewicz
Comment Utility
actually you will need to use
ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.UpdatePanel1.GetType(), "RestorePosition" + DateTime.Now.Ticks, "RestoreScrollPosition()", true);
0
 

Author Comment

by:siddiqishahid
Comment Utility
I am getting following error
"Conversion from string "RestorePosition" to type 'Double' is not valid."

It worked fine and got expected when I added .ToString  after DateTime.Now.Ticks, in following script,

ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.UpdatePanel1.GetType(), "RestorePosition" + DateTime.Now.Ticks.ToString, "RestoreScrollPosition()", true);

Is that OK?

I am also getting following error in javascript but i got desired result.
Line: 93
Char: 24
Error: Expected ';'
Code:0

I did viewsource and tried to locate that error but could not figure out? Any suggestions?

Thanks.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head><title>

	Untitled Page

</title>

    <script type="text/javascript">

    function SaveScrollPosition()

    {

        pnl=document.getElementById('Panel1');

        hdn=document.getElementById('hdnScrollPosition');

        hdn.value=pnl.scrollTop;

    }

    function RestoreScrollPosition()

    {

        pnl=document.getElementById('Panel1');

        hdn=document.getElementById('hdnScrollPosition');

        pnl.scrollTop=hdn.value;

    }

    </script>

</head>

<body>

    <form name="form1" method="post" action="Default.aspx" id="form1">

<div>

<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />

<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />

<input type="hidden" name="__LASTFOCUS" id="__LASTFOCUS" value="" />

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTYzMjc5MjI4MmQYAQUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgIFCUNoZWNrQm94MQUJQ2hlY2tCb3gywOFFxJDMSYQyMA8nWgwm1o3aeGQ=" />

</div>
 

<script type="text/javascript">

<!--

var theForm = document.forms['form1'];

if (!theForm) {

    theForm = document.form1;

}

function __doPostBack(eventTarget, eventArgument) {

    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {

        theForm.__EVENTTARGET.value = eventTarget;

        theForm.__EVENTARGUMENT.value = eventArgument;

        theForm.submit();

    }

}

// -->

</script>
 
 

<script src="/BPXA/WebResource.axd?d=1SnFzSJnzo-c1BMcPfq1rQ2&amp;t=633407563850353049" type="text/javascript"></script>
 
 

<script src="/BPXA/ScriptResource.axd?d=YvPGwM0clA7GDcymQg_2lS1jr8yv_CqgZiHkDGYZoy_kQ2XQDfR0y7ZQ8QMgoR5bhvDBaTzX7J1TzRVeStIA1cDL_LaU12JE3GVoX6us6G01&amp;t=633413469343099995" type="text/javascript"></script>

<script src="/BPXA/ScriptResource.axd?d=YvPGwM0clA7GDcymQg_2lS1jr8yv_CqgZiHkDGYZoy_kQ2XQDfR0y7ZQ8QMgoR5bhvDBaTzX7J1TzRVeStIA1VjFkDNP7vVJqyJtK-2130Sp7uREkT-EisZrNPtG6IjS0&amp;t=633413469343099995" type="text/javascript"></script>

        

        <div>

            &nbsp;<script type="text/javascript">

//<![CDATA[

Sys.WebForms.PageRequestManager._initialize('ScriptManager1', document.getElementById('form1'));

Sys.WebForms.PageRequestManager.getInstance()._updateControls(['tUpdatePanel1'], [], [], 90);

//]]>

</script>
 

          

        </div>

        <div id="UpdatePanel1">

	

            <div id="Panel1" onscroll="SaveScrollPosition();" style="height:184px;width:376px;overflow-y:scroll;z-index: 100;

                left: 0px; position: absolute; top: 0px">

		

                <input name="TextBox1" type="text" id="TextBox1" style="z-index: 100; left: 16px; position: absolute;

                    top: 24px" />

                <input name="TextBox2" type="text" id="TextBox2" style="z-index: 101; left: 16px; position: absolute;

                    top: 64px" />

                <input name="TextBox3" type="text" id="TextBox3" style="z-index: 102; left: 16px; position: absolute;

                    top: 104px" />

                <input name="TextBox4" type="text" id="TextBox4" style="z-index: 103; left: 16px; position: absolute;

                    top: 144px" />

                <input name="TextBox5" type="text" id="TextBox5" style="z-index: 104; left: 120px; position: absolute;

                    top: 280px" />

                <span style="z-index: 105;

                    left: 8px; position: absolute; top: 280px"><input id="CheckBox1" type="checkbox" name="CheckBox1" onclick="javascript:setTimeout('__doPostBack(\'CheckBox1\',\'\')', 0)" /></span>

                <input name="TextBox6" type="text" id="TextBox6" style="z-index: 106; left: 32px; position: absolute;

                    top: 440px" />

                <input name="TextBox7" type="text" id="TextBox7" style="z-index: 107; left: 32px; position: absolute;

                    top: 472px" />

                <span style="z-index: 109;

                    left: 208px; position: absolute; top: 440px"><input id="CheckBox2" type="checkbox" name="CheckBox2" onclick="javascript:setTimeout('__doPostBack(\'CheckBox2\',\'\')', 0)" /></span>

                    

            

	</div>

            

</div>

     

        <input type="hidden" id="hdnScrollPosition">

    

<div>
 

	<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWCgLy0f6ODALs0bLrBgLs0fbZDALs0Yq1BQLs0e58AuzRgtgJAoLk17sJAuzRxsYPAuzR2qEIAv/j17sJnVn4KOJjxRDepINYarK8KGmDapY=" />

</div>
 

<script type="text/javascript">

<!--

RestoreScrollPosition()Sys.Application.initialize();

// -->

</script>

</form>

</body>

</html>

Open in new window

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 16

Accepted Solution

by:
burakiewicz earned 250 total points
Comment Utility
yeah that is fine, in C# it must auto convert it
you need a ; after the RestoreScrollPosition(),
ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.UpdatePanel1.GetType(), "RestorePosition" + DateTime.Now.Ticks.ToString, "RestoreScrollPosition();<----------", true);

so use this
ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.UpdatePanel1.GetType(), "RestorePosition" + DateTime.Now.Ticks.ToString, "RestoreScrollPosition();", true);

0
 
LVL 16

Expert Comment

by:burakiewicz
Comment Utility
sorry i forgot it in my code
0
 

Author Comment

by:siddiqishahid
Comment Utility
It works perfectly fine if I put alert script in following function,

function RestoreScrollPosition()
    {
       alert('anything');
        pnl=document.getElementById('Panel1')
        hdn=document.getElementById('hdnScrollPosition')
        pnl.scrollTop=hdn.value;
    }

if I put alert it perfectly fine. I am surprise what alert can make difference?

Thanks.
0
 
LVL 16

Expert Comment

by:burakiewicz
Comment Utility
try adding the ; to all the items-- dangit
but not sure why it would work with an alert in there
function RestoreScrollPosition()
    {
       pnl=document.getElementById('Panel1');
        hdn=document.getElementById('hdnScrollPosition');
        pnl.scrollTop=hdn.value;
    }
0
 

Author Comment

by:siddiqishahid
Comment Utility
It has been fixed now by adding few lines in procedures. Thanks for your help.
function SaveScrollPosition()
        {  
            window.setTimeout("SetNow('" + document.getElementById("myHidden").innerHtml + "')",2000);
            pnl=document.getElementById('Panel1');
            hdn=document.getElementById('hdnScrollPosition');
            hdn.value=pnl.scrollTop;
        }
       
        function RestoreScrollPosition()
        {    
            document.getElementById("myHidden").innerHtml = document.getElementById("Panel1").scrollTop;        
            pnl=document.getElementById('Panel1');
            hdn=document.getElementById('hdnScrollPosition');        
            pnl.scrollTop=hdn.value;
        }
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
This article will show, step by step, how to integrate R code into a R Sweave document
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now