How do I call a javascript file on my sitemaster? C #

1

As I command to call a .js file from my site master so that it is the first to be executed within my application.

SiteMaster code:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="sitemaster.master.cs" Inherits="sitemaster" %>

<!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>

<script type="text/javascript" src="<%= ResolveUrl("login.js") %>"></script>
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

JS code

$(document).ready(function () {

    alert("Entro al JS");

});

But when running the application I do not get any message. And the location of the file is as follows:

I have also used the line of my script in the sitemaster as follows:

<script type="text/javascript" src="<%= ResolveUrl("~/login.js") %>"></script>

But neither of the two ways has worked for me. Another option he used was the classic one:

<script type="text/javascript" src="login.js"></script>

Also add jquery to my sitemaster's code, add it up in the head and call my file .js call it at the end of my body:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

If I add the lines of jquery and javascript code directly in my aspx (the view) in a traditional way it works correctly, but in the sitemaster I do it in the same order the two script links but it does not send me the message.

Code of login.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="login" %>

<!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>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
        <script type="text/javascript" src="login.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="Label1" runat="server" Text="Hola Mundo"></asp:Label>
    </div>
    </form>
</body>
</html>
    
asked by David 24.03.2017 в 19:55
source

2 answers

2

You need the reference to jQuery , add it after title.

I put one that points to Google, replace it and use the one from your local jQuery installation.

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="sitemaster.master.cs" Inherits="sitemaster" %>

<!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>

// Agrega aqui tu link a jQuery
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>

<script type="text/javascript" src="<%= ResolveUrl("login.js") %>"></script>
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

It is also necessary that you modify your login.aspx by something like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="login" MasterPageFile="~/sitemaster.master" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
<!-- Aqui van los CSS, JS y demas de tu pagina login-->
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <div>
    <h1>Test</h1>
    </div>
</asp:Content>

MasterPages are templates in which you define which will be filled out by the pages using the MasterPage as a template.

In the example above we added the MasterPageFile to force login.aspx to use the master page.

To test you have to run the application and go to login.aspx and then the MasterPager will appear with the content of login.aspx.

    
answered by 24.03.2017 / 20:01
source
0

I think you just need to place the physical route where your js is located. I recommend you load it before closing the body tag. Greetings!.

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="sitemaster.master.cs" Inherits="sitemaster" %>

<!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>
    <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

    </asp:ContentPlaceHolder>
</div>
</form>
<script type="text/javascript" src="login.js"></script>

</body>
</html>
    
answered by 24.03.2017 в 20:01