Solved

Maintain scrolling in Panel after postback

Posted on 2008-06-18
10
4,173 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why is "packages element is not declared"? 2 124
WPF - Tooltips for ComboBox items 5 28
Jquery autocomplete 10 68
Interview questions for support of a legacy ASP.NET site 4 37
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

932 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

13 Experts available now in Live!

Get 1:1 Help Now