Can't you just place the script in the MasterPage above the ContentPlaceHolder in which the contents of the page and the user control is rendered? Then the js code inside the ContentPlaceHolder will find the references declared in the master page.
Master:
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="WebApplication1.UC.Site1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
well all the javascript part is execute in the client side only so i dont think the sequence even matters as when we get the page back from server all the code rendered and nothing remains pending.
Yarden
Member
559 Points
129 Posts
User Controls and JavaScript and Master Pages
Apr 27, 2012 08:16 PM|LINK
Hi,
Consider the following situation:
There is a master page with a contentPlaceHolder for all javascript...
for speed issuses this section is at the bottom of the page.
my user control generates some javascript that make use of some references in the master page javascript (Jquery library)
so, if my user control is rendered before the master page javascript, it won't work.
and this is my question:
how do I make some javascript code block inside my .ascx file to be rendered to the asp:Content JavaScript in the .aspx page
or maybe my all thinking is worng?
Thanks!
UserControls javascript masterpage
mm10
Contributor
6445 Points
1187 Posts
Re: User Controls and JavaScript and Master Pages
Apr 28, 2012 03:33 PM|LINK
Can't you just place the script in the MasterPage above the ContentPlaceHolder in which the contents of the page and the user control is rendered? Then the js code inside the ContentPlaceHolder will find the references declared in the master page.
Master:
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="WebApplication1.UC.Site1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<script language="javascript" type="text/javascript">
var i = 0;
</script>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
Page:
<%@ Page Title="" Language="C#" MasterPageFile="~/UC/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.UC.WebForm1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<script type="text/javascript" language="javascript">
alert(i); //i declared above in MasterPage is 0 here...
</script>
</asp:Content>
Yarden
Member
559 Points
129 Posts
Re: User Controls and JavaScript and Master Pages
Apr 28, 2012 05:20 PM|LINK
sure I can do that. this is how it's done now.
But, my problem is that im not always use this User Control (it is appended to the page dynamically)
that is why, i want the JavaScript that is related to it to be excuted only if the user control is called.
Understand now? :)
the perfect solution would be if I could set a the master page in the <%@ Control %> like in a aspx page.
and inside the user control put the JS inside a asp:content in the master page..
rajanchaudha...
Member
24 Points
7 Posts
Re: User Controls and JavaScript and Master Pages
Apr 28, 2012 05:24 PM|LINK
well all the javascript part is execute in the client side only so i dont think the sequence even matters as when we get the page back from server all the code rendered and nothing remains pending.
so the problem must be something else
Yarden
Member
559 Points
129 Posts
Re: User Controls and JavaScript and Master Pages
Apr 28, 2012 08:29 PM|LINK
the sequence matters.
if my code tries to use jquery libarary before the reference to it, bugs will occur..
ive got a nice answer here http://stackoverflow.com/questions/10365822/user-controls-and-javascript-and-master-pages
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: User Controls and JavaScript and Master Pages
Apr 29, 2012 12:57 AM|LINK
Congratulation!Welcome to our forum next time to share more ideas or solutions……
Reguards!