Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
my website has a master page, and when creating a new page, it takes the master page setting - and that is OK - but the problem is that i want to include a javascript code to this page and i cant add it because i can edit the body only through contentplaceholder that can be edited on this page, and in the contentplaceholder we cannot add javascript code, because the <script> tag should be before the body, now i tried also to include it in the master page before the body but it gave me errors because in the function i used some code that deals with the buttons of that page and it's not included in the master page... does anyone has a solution for this problem..
Posted
Comments
Sunasara Imdadhusen 14-Feb-11 1:08am    
Please provide snippet of code?
Albin Abel 14-Feb-11 1:15am    
In my example if you don't want the button become a submit button then set this property for the button UseSubmitBehavior="false"

Use Sandeep's answer if you want to add scripts at runtime

For example if you add a script in the master page

XML
<script type="text/javascript">
    function test() {
        alert("It Works");
    }
</script>


Then can assign to a button in other pages like these ways

XML
<asp:Button ID="Button1" runat="server" Text="Button"  OnClientClick="test();"/>

OR from code behind

C#
Button1.Attributes.Add("onclick", "test();");
 
Share this answer
 
Comments
shadi_abushaar 14-Feb-11 1:22am    
i did exactly what you are telling me from the beginning but the problem is that when i put the code in a separate page it works, but when i include it in the page that has a master page it doesn't work...
Albin Abel 14-Feb-11 1:42am    
That's right. The script need to move to the head section. If Master page is there the head section is in the master page. Use Sandeep's answer to inject the script to the head section when the page loads or if you want to add it in the body itself then use Sunasara Imdadhusen's answer. Is that fine?
Hi,

You can include Javascript in .Master page like:
XML
<head id="Head1" runat="server">
    <title>My Master Page</title>
   
    <script language="JavaScript" type="text/javascript">
function jsfunction()
{
}
</script>
</head>


or define in Content .aspx page like:
XML
<asp:UpdatePanel ID="GridContainer" runat="server">
        <ContentTemplate>
        </ContentTemplate>
        <Triggers>
        </Triggers>
    </asp:UpdatePanel>
    <script language="JavaScript" type="text/JavaScript">
function myjsfunction()
{
}
     </script>
</asp:Content>


Thanks,
Imdadhusen
 
Share this answer
 
Comments
shadi_abushaar 14-Feb-11 2:05am    
it's working but i dont know how :)
Sunasara Imdadhusen 14-Feb-11 2:27am    
What you mean to say?
Try using Page.RegisterClientScriptBlock Method [^] OR Page.RegisterStartupScript Method[^]

Using them, you can inject the scripts into a page from server side and based on certain condition (and specific to page.)

For samples on how to use it: How to Use RegisterClientScriptBlock & RegisterStartupScript[^]
Sample 2[^]
 
Share this answer
 
Comments
Albin Abel 14-Feb-11 1:17am    
my 5
Albin Abel 14-Feb-11 1:23am    
In recent versions of VS the syntax changed to Page.ClientScript.RegisterStartupScript and Page.ClientScript.RegisterClientScriptBlock. The previous syntax also stll have support but obsolete

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900