Solved

Masterpage javascripts conflicts with childpages javascripts

Posted on 2013-06-03
4
141 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 52

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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

930 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now