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.
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>
<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>
you can store the position of the panel in a hiddenfield and on the page load reset the position with javascript
ASKER
Thanks for your reply. Can you give me sample code?
Thanks.
Thanks.
javascript is first, on your panel add onscroll="SaveScrollPositi on();"
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.RegisterStart upScript(t his.GetTyp e(), "RestorePosition" + DateTime.Now.Ticks, "RestoreScrollPosition()", true);
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.RegisterStart
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>
actually you will need to use
ScriptManager.RegisterStar tupScript( this.Updat ePanel1, this.UpdatePanel1.GetType( ), "RestorePosition" + DateTime.Now.Ticks, "RestoreScrollPosition()", true);
ScriptManager.RegisterStar
ASKER
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.RegisterStar tupScript( this.Updat ePanel1, this.UpdatePanel1.GetType( ), "RestorePosition" + DateTime.Now.Ticks.ToStrin g, "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.
"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.RegisterStar
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&t=633407563850353049" type="text/javascript"></script>
<script src="/BPXA/ScriptResource.axd?d=YvPGwM0clA7GDcymQg_2lS1jr8yv_CqgZiHkDGYZoy_kQ2XQDfR0y7ZQ8QMgoR5bhvDBaTzX7J1TzRVeStIA1cDL_LaU12JE3GVoX6us6G01&t=633413469343099995" type="text/javascript"></script>
<script src="/BPXA/ScriptResource.axd?d=YvPGwM0clA7GDcymQg_2lS1jr8yv_CqgZiHkDGYZoy_kQ2XQDfR0y7ZQ8QMgoR5bhvDBaTzX7J1TzRVeStIA1VjFkDNP7vVJqyJtK-2130Sp7uREkT-EisZrNPtG6IjS0&t=633413469343099995" type="text/javascript"></script>
<div>
<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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
sorry i forgot it in my code
ASKER
It works perfectly fine if I put alert script in following function,
function RestoreScrollPosition()
{
alert('anything');
pnl=document.getElementByI d('Panel1' )
hdn=document.getElementByI d('hdnScro llPosition ')
pnl.scrollTop=hdn.value;
}
if I put alert it perfectly fine. I am surprise what alert can make difference?
Thanks.
function RestoreScrollPosition()
{
alert('anything');
pnl=document.getElementByI
hdn=document.getElementByI
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.getElementByI d('Panel1' );
hdn=document.getElementByI d('hdnScro llPosition ');
pnl.scrollTop=hdn.value;
}
but not sure why it would work with an alert in there
function RestoreScrollPosition()
{
pnl=document.getElementByI
hdn=document.getElementByI
pnl.scrollTop=hdn.value;
}
ASKER
It has been fixed now by adding few lines in procedures. Thanks for your help.
function SaveScrollPosition()
{
window.setTimeout("SetNow( '" + document.getElementById("m yHidden"). innerHtml + "')",2000);
pnl=document.getElementByI d('Panel1' );
hdn=document.getElementByI d('hdnScro llPosition ');
hdn.value=pnl.scrollTop;
}
function RestoreScrollPosition()
{
document.getElementById("m yHidden"). innerHtml = document.getElementById("P anel1").sc rollTop;
pnl=document.getElementByI d('Panel1' );
hdn=document.getElementByI d('hdnScro llPosition ');
pnl.scrollTop=hdn.value;
}
function SaveScrollPosition()
{
window.setTimeout("SetNow(
pnl=document.getElementByI
hdn=document.getElementByI
hdn.value=pnl.scrollTop;
}
function RestoreScrollPosition()
{
document.getElementById("m
pnl=document.getElementByI
hdn=document.getElementByI
pnl.scrollTop=hdn.value;
}