Solved

Masterpage javascripts conflicts with childpages javascripts

Posted on 2013-06-03
4
144 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 56

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 56

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…

713 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