Solved

Masterpage javascripts conflicts with childpages javascripts

Posted on 2013-06-03
4
146 Views
Last Modified: 2013-06-07
Is it a good idea to work with Masterpages?
I realized there are javascript conflicts between masterpages and its childpages.
.
For example, if masterpage and childpage have a javascript function with same name, then a conflict shows up and childpage only recognizes  javascript function of masterpage.

For example, in MasterPage I have to use function name like "mouseOverImage_" and in childpage I have to use function name like "mouseOverImage"

'In MasterPage.aspx:
<head runat="server">
 <script type="text/javascript">

	function mouseOverImage_(ctl) {
	    var CtlName = ctl.name.toUpperCase();

	    switch (true) {
	        case (ctl.name.toUpperCase().indexOf("buttonAdmin".toUpperCase()) > 0):
	            ctl.src = 'images/Master/btn_Admin_over.png';
	            break;
	        default:
	            //Nothing;
	    }
	}
 </script>
</head> 
'In ChildPage.aspx a similar javascript function has to be named differently like: mouseOverImage

Open in new window


How to tell childpage to not consider MasterPage javascript "mouseOverImage_" ?


Other concern is that in child page, names of asp controls get changed. For example, in childpage, buttonAdmin gets renamed as:

ctl00$Main_Content$buttonAdmin

And in childpage this javascript command  won't work

document.getElementById("buttonAdmin").src = "images/orange-round-button-th.png";

Open in new window


because buttonAdmin was renamed as "ctl00$Main_Content$buttonAdmin"
0
Comment
Question by:miyahira
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 58

Assisted Solution

by:Julian Hansen
Julian Hansen earned 100 total points
ID: 39218658
You are asking this the wrong way round - don't think of it in terms of Master and Child pages - think of it in terms of the page that is actually rendered.

The Master / Child pages are just a tool to allow you to display content on different pages within a common frame - and not having to replicate the frame.

The rule therefore is don't create functions of the same name.

My question is why do you want to have two functions of the same name?
0
 
LVL 1

Author Comment

by:miyahira
ID: 39219206
Ok. You have a point, it is easy to just not create functions with the same name.

However, as I said, javascript code on child page will not work normally since names of childpage's controls get horrible changed. It would be nice if those control names won't changed. How you deal with that?
0
 
LVL 14

Accepted Solution

by:
dejaanbu earned 400 total points
ID: 39221588
childpage's controls get horrible changed.

That's the way Master and child pages work...For accessing the controls by its name you have to use its ClientId.

that is, u have to use

document.getElementById('<%=buttonAdmin.ClientID%>').src = "images/orange-round-button-th.png";

Open in new window


if you dont want to do that, simply set the "ClientIDMode" Property of that button to "Static".. pbm will be solved..

more details here:
http://www.dotnetfunda.com/articles/article838-aspnet-40-manipulate-clientid-using-clientidmode.aspx 

and here:
http://www.unknownroad.com/rtfm/ASPNET/MasterPages.html

here too:
http://beyondrelational.com/modules/2/blogs/61/posts/11206/all-about-client-id-mode-in-aspnet-4.aspx
0
 
LVL 58

Assisted Solution

by:Julian Hansen
Julian Hansen earned 100 total points
ID: 39221648
My advice is when using javascript stay away from any specific environment conventions like .Net.

As deejanbu said above use the control's id - this is the fastest method of accessing an html element from js anyway.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

628 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