Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Masterpage javascripts conflicts with childpages javascripts

Posted on 2013-06-03
4
Medium Priority
?
149 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
  • 2
4 Comments
 
LVL 60

Assisted Solution

by:Julian Hansen
Julian Hansen earned 400 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 1600 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 60

Assisted Solution

by:Julian Hansen
Julian Hansen earned 400 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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

886 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