Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with Javascript in Master Page

Status
Not open for further replies.

stark8352

Programmer
Jan 29, 2008
3
GB
Please can you help!

I have a web application that uses javascript to create a scroll bar effect for my gridview but also maintains scroll position in certain circumstances. The maintain scroll position is triggered based on the value of a session variable called "shouldscroll". I am using Page Init event to add the maintain scroll position functionaility to the scroll event. I am using Visual Web Developer 2005 and VB.

This works fine when all of the code (VB parts and javascript) are all in one aspx page but I run into problems when I try to use a Master Page and Content Page. The scroll effect still works but I can no longer get the maintain scroll position functionality to work. Can anyone help with this? Below is some of the relevant code, sorry it's so long if I can provide it to anyone in any better way please let me know:

MASTER PAGE:

Code:
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) 
Me.GridViewScrollSalesDetails2.Attributes.Add("onscroll", "javascript:setscrollPos();") 
End Sub 

<style type="text/css"> 
#GridViewScrollSalesDetails2 
{ 
width: 73%; height: 100px; overflow: auto; margin-left: 5px; 
} div#GridViewScrollSalesDetails2 table th 
{ 
width: 100px; 
} #GridViewScrollSalesDetails2 th 
{ 
top: expression(document.getElementById( "GridViewScrollSalesDetails2" ).scrollTop-2); 
z-index: 20; 
} #GridViewScrollSalesDetails2 td.locked, #GridViewScrollSaleDetails2 th.locked 
{ 
left: expression(document.getElementById( "GridViewScrollSalesDetails2" ).scrollLeft); 
position: relative; z-index: 100; border: solid 1px white; 
} 
#GridViewScrollSalesDetails2 th, #GridViewScrollSalesDetails2 th.locked 
{ 
position: relative; cursor: default; border: solid 1px white; 
} 
#GridViewScrollSalesDetails2 th.locked 
{ 
z-index: 110; 
} 
</style> 

<script type="text/javascript"> 
<!--window.onload = function () 
{ 
var shouldScroll = '<%= session("shouldScroll") %>'; 
if ( shouldScroll.toUpperCase() != 'YES' ) 
return; 
var strCook = document.cookie; 
if ( strCook.indexOf("!~") !=0 ) 
{ 
var intS = strCook.indexOf("!~"); 
var intE = strCook.indexOf("~!"); 
var strPos = strCook.substring(intS+2, intE); 
document.body.scrollTop = strPos; 
} 
// This condition will set scroll position od <div>. 
if ( strCook.indexOf("!*") != 0 ) 
{ 
var intdS = strCook.indexOf("!*"); 
var intdE = strCook.indexOf("*!"); 
var strdPos = strCook.substring(intdS+2,intdE); 
document.getElementById('GridViewScrollSalesDetails2').scrollTop = strdPos; 
} 
} 
function setScrollPosition() 
{ 
var intY = document.body.scrollTop; 
document.cookie = "yPos=!~" + intY + "~!"; 
} 
window.onscroll = setScrollPosition; 
function setScrollPos() 
{ 
var divY = document.getElementById('GridViewScrollSalesDetails2').scrollTop; 
document.cookie = "divPos=!*" + divY + "*!"; 
} 
// --> 
</script> 


<form id="form1" runat="server"> 
<div id="GridViewScrollSalesDetails2" runat="server"> 
&nbsp; &nbsp;<br /> 
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server" > 
</asp:contentplaceholder> 
<br /> 
&nbsp;</div> 
</form> 

CONTENT PAGE: 

<div id="GridViewScrollSalesDetails2"> 
<asp:GridView............ </GridView> 
</div> 

Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) 

If e.CommandName = "Edit" Then 
Session("shouldScroll") = "YES" 
Else 
End If

I'm not sure whether perhaps having changed the value of "shouldscroll" to Yes in the GridView1_RowCommand above in the content page whether it needs to somehow call the javascript function again from the Master Page?

Something I've noticed about the code aswell is that in order to assign the onscroll event in Page init to the GridViewScrollSalesDetails2, I have to put runat="server" within the <div> tag surrounding the Gridview being affected (when it was in a normal aspx page). When using Master Pages and Content pages I can't use runat="server" in the content page as this causes the scroll facility to not appear at all. I have therefore put the runat="server" in the <div> tag surrounding the contentplace holder instead - not sure if this is correct but it doesn't work anyway.

Just to re-iterate I can get the scroll effect to work between Master page and Content Page but not the maintain scroll position part of it.

Does anybody know what I'm doing wrong and if this is even possible. Maybe I have to revert back to a normal aspx page for the pages affected by this but this means duplicating code!!!
Thanks

Steven
 
Dan,

Do you know how my code would change to support client side code or do you have any links to articles that will explain what I'm trying to achieve in client side code.

Thanks

Steven
 
When you see the web page displayed in the browser, click on View, then Source. Copy that and show us the relevant parts here.

Lee
 
Lee/Dan,

Below is some of the client side code as you requested. I hope I've given you all the relevant bits. I did what you
said and run the page in the browser and this code was produced after I clicked the edit button of a
row in a gridview control after scrolling. It did not maintain the scroll position.

Code:
<script type="text/javascript">
<!--
window.onload = function () 
{
 var shouldScroll = 'NO'; 
 if ( shouldScroll.toUpperCase() != 'YES' )
  return; 

 var strCook = document.cookie;

 if ( strCook.indexOf("!~") !=0 ) 
 { 
  var intS = strCook.indexOf("!~"); 
  var intE = strCook.indexOf("~!"); 
  var strPos = strCook.substring(intS+2, intE); 
  document.body.scrollTop = strPos;
 } 

 // This condition will set scroll position od <div>.
 if ( strCook.indexOf("!*") != 0 ) 
 { 
  var intdS = strCook.indexOf("!*"); 
  var intdE = strCook.indexOf("*!"); 
  var strdPos = strCook.substring(intdS+2,intdE); 
  document.getElementById('"ctl00_GridViewScrollSalesDetails2"').scrollTop = strdPos; 
 }
}

function setScrollPosition() 
{ 
 var intY = document.body.scrollTop; 
 document.cookie = "yPos=!~" + intY + "~!"; 
}
window.onscroll = setScrollPosition;

function setScrollPos() 
{
 var divY = document.getElementById('"ctl00_GridViewScrollSalesDetails2"').scrollTop; 
 document.cookie = "divPos=!*" + divY + "*!"; 
} 
// -->
</script>
<script type="text/javascript" src="JScript.js">
</script>
<html>
<head><link rel="stylesheet" type="text/css" href="menustyle.css" />
<style type="text/css">

#GridViewScrollSalesDetails2
{
    width: 73%;
    height: 100px;
    overflow: auto;
    margin-left: 5px;
} div#GridViewScrollSalesDetails2 table th
{
    width: 100px;
} #GridViewScrollSalesDetails2 th
{
    top: expression(document.getElementById(   "GridViewScrollSalesDetails2" ).scrollTop-2);
    z-index: 20;
} #GridViewScrollSalesDetails2 td.locked, #GridViewScrollSaleDetails2 th.locked
{
    left: expression(document.getElementById(   "GridViewScrollSalesDetails2" ).scrollLeft);
    position: relative;
    z-index: 100;
    border: solid 1px white;
} 
#GridViewScrollSalesDetails2 th, #GridViewScrollSalesDetails2 th.locked
{
    position: relative;
    cursor: default;
    border: solid 1px white;
}
#GridViewScrollSalesDetails2 th.locked
{
    z-index: 110;
}
</style>

<link href="../App_Themes/Blue/StyleSheet.css" type="text/css" rel="stylesheet" /><title>
	Untitled Page
</title></head>
<body>
    <form name="aspnetForm" method="post" action="MembersSalesDetails.aspx?SalesDocNum=SIN00011&amp;SalesDocType=Product&amp;Posted=No" id="aspnetForm">
<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="0AF/ue/0OrfKFwToHX4BBjEZyLAokr6XAmd85FWwCKfd5o+bDCfWV41yZ6QRpc+QNx1SHib/A1Wx76v00T1jJ0lkKrzYsigQIkZlkbu7mreQvF7MB5QUMtx/A8rzg1WlBNLZUEZEZIGr64i1e9MUNbgB6TshZLKFAmd20bjESwOdHPpBHyC+9keURuUOx+0ctytjkO7LxKpagvhlyKSl9ka8IsVI1EoWBGRXwUYkKnI3C/+UPIjPa7AyZGdpEjj6aq0M543VY3UBRp0uBDkB8lByZDx3Q9yg3XCvKv6wSCvnFHzUcfsitw2QLqqt2e4vAeffVN7oKyZ7H9H8evAnpVtADYByOYmewj1bhJtrQyF0bGsdLCeXu59Fmvv+3QnPSvkV2+9XC2Egk/3ppK7OkYTUHt95qNbAhCq/4VRq+rV8tt4QdGqE282Rx8cHgk2F1WkVcYHLeG5bjq0AoKdr2nZ0+SCTvFq+OHl/F5YAUDcELzaGvmA4IHAruOsjN1c46bKeGfsscz0lhQDI9XDgkakWR7jOYYT/nknOJDbtD5eeR7J0Zd8bbNsdWnftBkyxdzBqUHP5f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URpEXpK3KtoffS0rWXeqJVNiMfNZz9/UhwCiyiAmW8BZi8FKxMcHcC/RL/5ykyrMo3cqi9COaQ7S047aGib0WAoWdsnE2+YQYKCacGuN6ewk+e+K/Mlaz8hgr6UPzotldt8OEEhwnjAqhFm4At4nB0K1wn+/T8N6HMESySmkZyg+K9wy80nPZVPYgA4afMP6W4uruMawYJ3TXl8MJqTC5mhOA2JNobbEPO0r3t4Q91pPz22i/d4MRRPHXpnHXhLCUGmT2Tz7S0RMlHkK31IsX/9Ffm7QjSt3i50FCQcoNadO9iBAqMis5GUB0JANytUk4cnW0HX+jIWpuv/bCFHMHWwssvWBEb/AEqbMfEGUK8b0AqBiZNNQIH23uaGaTXneRg6clyw5ImjCSMj87A65MmZgFZmJrzO6PvZ2COZ/eaySNPs7j2DOCOFHPGIj5RWw3VE8StBEd4/UKqSqt/d1CeNnvJR7XqxjHEzCceuUxraMe+Byqk9rg5wWZrasXn112cLOGJ4M4d+g==" />
</div>

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['aspnetForm'];
if (!theForm) {
    theForm = document.aspnetForm;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>


<script src="/WebDevelop_141107/WebResource.axd?d=JpVtMDRuS3g3i4kn4oNaBg2&amp;t=633363484966875000" type="text/javascript"></script>

   <div id="ctl00_GridViewScrollSalesDetails2" onscroll="javascript:setscrollPos();">
    
        &nbsp; &nbsp;<br />
        
        <br />
        <br />
        <br />
        <br />
    
        <br />
        <br />
        <span id="ctl00_ContentPlaceHolder1_SalesDocType" tabindex="-1" style="display:inline-block;color:Blue;font-size:25pt;font-weight:bold;height:32px;width:209px;">Sales Invoice</span>
        
        &nbsp;
        <br />
        <table tabindex="-1" cellspacing="2" cellpadding="3" rules="all" border="1" id="ctl00_ContentPlaceHolder1_FormView1" style="background-color:White;border-color:White;border-width:1px;border-style:Solid;height:50px;width:610px;">
	<tr>
		<td colspan="2">
                DocNo: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
                <span id="ctl00_ContentPlaceHolder1_FormView1_SalesDocNumLabel" style="display:inline-block;color:Blue;background-color:White;font-weight:bold;width:80px;">SIN00011</span>
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;
                &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;
                Date:&nbsp; &nbsp;&nbsp; 
                
                <span id="ctl00_ContentPlaceHolder1_FormView1_Label10" style="display:inline-block;color:Blue;background-color:White;font-weight:bold;width:80px;">20/11/2007</span><br />
                <br />
                CustomerCode: &nbsp; &nbsp;
                <span id="ctl00_ContentPlaceHolder1_FormView1_CustomerCodeLabel" style="display:inline-block;color:Blue;background-color:White;font-weight:bold;width:45px;">C1010</span>
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
                &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; Period: &nbsp;
                <span id="ctl00_ContentPlaceHolder1_FormView1_PeriodLabel" style="display:inline-block;color:Blue;background-color:White;font-weight:bold;width:15px;">11</span>
                &nbsp;&nbsp;&nbsp;&nbsp;<br />
                <br />
                Doc Type: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span id="ctl00_ContentPlaceHolder1_FormView1_SalesDocTypeLabel" style="display:inline-block;color:Blue;background-color:White;font-weight:bold;width:45px;">Product</span>
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Posted: &nbsp;&nbsp;&nbsp;<span id="ctl00_ContentPlaceHolder1_FormView1_PostedLabel" style="display:inline-block;color:Blue;background-color:White;font-weight:bold;width:15px;">No</span>
                &nbsp; &nbsp;&nbsp;<br />
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
            </td>
	</tr>
</table>

There was a lot more code than this so if I've missed anything obvious let me know.

Thanks

Steven
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top