Solved

Maintain scrolling in Panel after postback

Posted on 2008-06-18
10
4,249 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
ID: 21814174
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
ID: 21814371
Thanks for your reply. Can you give me sample code?

Thanks.
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 21814790
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 16

Expert Comment

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

Author Comment

by:siddiqishahid
ID: 21815359
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
 
LVL 16

Accepted Solution

by:
burakiewicz earned 250 total points
ID: 21815411
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
ID: 21815424
sorry i forgot it in my code
0
 

Author Comment

by:siddiqishahid
ID: 21815904
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
ID: 21815987
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
ID: 21816917
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

820 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