Link to home
Start Free TrialLog in
Avatar of siddiqishahid
siddiqishahid

asked on

Maintain scrolling in Panel after postback

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

Avatar of burakiewicz
burakiewicz
Flag of United States of America image

you can store the position of the panel in a hiddenfield and on the page load reset the position with javascript
Avatar of siddiqishahid
siddiqishahid

ASKER

Thanks for your reply. Can you give me sample code?

Thanks.
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

actually you will need to use
ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.UpdatePanel1.GetType(), "RestorePosition" + DateTime.Now.Ticks, "RestoreScrollPosition()", true);
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

ASKER CERTIFIED SOLUTION
Avatar of burakiewicz
burakiewicz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sorry i forgot it in my code
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.
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;
    }
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;
        }