How to use JavaScript in Master Page ContentPlace Holder in ASP.NET

February 04, 2016 , 0 Comments

In this article, I will explain you, How to use JavaScript code in ContentPlaceHolder of Master page. If your web page (Web Form=.aspx page)  is inherit from master page then you must to use ClientID of the control to get the value. Like


By using above mentioned code we want to retrieve inner html text of the label. If you are using java script in simple web form which is not inherit from master page then you can use simple id of the control as parameter. Like


How to use JavaScript in Master page's Content PlaceHolder

Step-1 :  Add a Master page also add web form.
Step-2 :  Do Inherit web form from master page.
Step-3 :  Write the below mentioned code in the content place holder of the web form.

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <input id="Button1" type="button" value="button" onclick = "check()"/>
    <script type = "text/javascript">
        function check()
             var label = document.getElementById("<%=Label1.ClientID%>");
             var textbox = document.getElementById("<%=TextBox1.ClientID%>");
             alert("Label Value " + label.innerHTML + "TextBox Value  " + textbox.value);

Note: Please mentioned javascript code at last position of the page.

Jacob Lefore

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google