nirh1989 nirh1989 - 1 year ago 61
ASP.NET (C#) Question

ASP.Net Master Page and Javascript issue

I got the following HTML code in my master page

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

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
<link href="Styles/MasterStyleSheet.css" rel="stylesheet" />
<script type="text/javascript" src="<%= Page.ResolveClientUrl("Scripts/ShoppingCartSideMenuJavaScript.js") %>"></script>

<asp:ContentPlaceHolder ID="head" runat="server">
<form id="form1" runat="server">

<div id="ShoppingCartSideMenu">
Just some text

<div class="wrapper">
<h1 id="SiteTitle">JEWELRY</h1>
<div id="menu">
<li><a href="Home.aspx">HOME</a></li>
<li><a href="Shop.aspx">SHOP</a></li>
<li><a href="About.aspx">ABOUT</a></li>
<li><a href="Terms and Conditions.aspx">Terms & Conditions</a></li>
<li><a href="Contact.aspx">CONTACT</a></li>
<li id="seperator">|
<a id="ShoppingCartBtn" onclick="ShoppingCartBtn_Click" runat="server">
<img class="ShoppingCart" src="/Images/Icons/Shopping_cart.png" />
<label id="CountCartItems">0</label>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

<div class="FooterWrapper">
<div id="FooterDiv1">
&copy; 2016 JEWELRY - Created by someone
<div id="FooterDiv2">
<a href="#">
<img id="FooterFacebook" class="Icons" alt="Facebook" src="/Images/Icons/facebook-logo.png" /></a>
<a href="#">
<img id="FooterTwitter" class="Icons" alt="Twitter" src="/Images/Icons/twitter-logo.png" /></a>
<a href="#">
<img id="FooterInstagram" class="Icons" alt="Instagram" src="/Images/Icons/instagram-logo.png" /></a>

I got this Javascript in a separate js file

var main = function () {
/* Push menu over by 350px over */
$('#ShoppingCartBtn').click(function () {
right: "0px"
}, 200);

/* Then push the menu back */
$('body').click(function () {
right: "-350px"
}, 200);


I tried every way suggested in this post: ASP.Net Master Page and File path issues

But nothing seems to work

Answer Source

You should use ResolveUrl instead and create url from the root like this.

Page.ResolveUrl ("~/Scripts/ShoppingCartSideMenuJavaScript.js")

Update: That means you have not referenced jQuery.js before using it. Try this.

<script type="text/javascript" src="<%= Page.ResolveUrl("path/to/jquery.js") %>"></script>    
<script type="text/javascript" src="<%= Page.ResolveUrl("~/Scripts/ShoppingCartSideMenuJavaScript.js") %>"></script>