-->

Thursday, February 4, 2016

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

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

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

document.getElementById("<%=Label1.ClientID%>");

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

document.getElementById("Label1");

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);
           
        }
    </script>
</asp:Content>

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


Read other related articles

Also read other articles

© Copyright 2013 Computer Programming | All Right Reserved