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">
mm10
Contributor
6409 Points
1184 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>