Connecting Tech Pros Worldwide Help | Site Map

Calling javascript function in masterpage file using RegisterClientScript problem

Member
 
Join Date: Oct 2006
Posts: 111
#1: May 27 '08
Hi all,

when I call the javascript method from code behind file of the same page it works fine.



Expand|Select|Wrap|Line Numbers
  1. ScriptManager.RegisterClientScriptBlock(this, Page.GetType(), "Scroll", "ScrollToPosition();", true); 
  2.  
But when i put the JavaScript method in the masterpage, there the problem occurs.



When I press the button1,

It scrolls to the new position and shows the alert message Script from master page.

When i press OK it returns back to the bottom ie. to the position of the button1



The master page coding is like this.

Expand|Select|Wrap|Line Numbers
  1. <asp:ScriptManager ID="ScriptManager1" runat="server">
  2. </asp:ScriptManager>
  3.  
  4.  
  5. <asp:UpdatePanel ID="UpdatePanel1" runat="server">
  6.     <ContentTemplate>
  7.  
  8.                     <script language="javascript" type="text/javascript">
  9.                             function ScrollToPosition()
  10.                             {
  11.                                 var pnlErrorMessage = document.getElementById('ctl00_ContentPlaceHolder1_pnlErrorMessage');
  12.                                 var x = pnlErrorMessage.offsetLeft;
  13.                                 var y = pnlErrorMessage.offsetTop;
  14.  
  15.                                  alert("Script from master page");
  16.  
  17.  
  18.                                 window.scrollTo(x, y);
  19.                             }
  20.  
  21.                     </script>
  22.  
  23.                     <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
  24.                     </asp:ContentPlaceHolder>
  25.                 </ContentTemplate>
  26.             </asp:UpdatePanel>
  27.         </div>
Why this happens?



How to rectify it.



Thanks in advance.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: May 27 '08

re: Calling javascript function in masterpage file using RegisterClientScript problem


As a full member now, you should know that we expect your code to be posted in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use the tags in future.

MODERATOR.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#3: May 27 '08

re: Calling javascript function in masterpage file using RegisterClientScript problem


Quote:

Originally Posted by baburk

But when i put the JavaScript method in the masterpage, there the problem occurs. When I press the button1, It scrolls to the new position and shows the alert message Script from master page. When i press OK it returns back to the bottom ie. to the position of the button1

Please show the client-side HTML/JavaScript code as it appears in your browser (View Source).
Reply