如何从另一个 aspx 页面中引用 MasterPage 中的 JavaScript 代码
How to reference JavaScript code in MasterPage from within another aspx page
简单问题:我如何引用母版页文件中的 JavaScript 代码(该文件与许多其他 ASPX 页面也在 同一文件夹 上),来自我的一个 ASPX 页面?
主页
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="JobRegister.master.cs" Inherits="JobRegister_Job" %>
<!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>Tec-NQ miJob Register</title>
<style type="text/css">
textarea { font-family: Arial; font-size: 10pt; }
a { white-space: nowrap; }
.tablestyle { background-color:#eeeeee; width:100%; margin-bottom:2px; padding:3px; }
.checkboxlist input { position:relative; left: 360px; }
.checkboxlist label { position:relative; left: -22px; }
.hidepanel { display: none; }
.showpanel { display: block; }
</style>
<script src="<%# ResolveUrl("~/") %>" type="text/javascript">
/*
Added : Variables required for image uploading & validation checking.
By : Amit Champaneri
On : 4th April 2008
*/
var hoverColor = "#00000b"; //DIV Color when mouse is hover the DIV
var defaultColor = "black"; //DIV default color
var selectColor = "#000000"; // DIV color when its selected.
var selectedDIV = ""; //ID of the DIV user has currently selected(it will be 1,2,3 or 4)
var objActiveX; //Object of Clipboard ActiveX control.
..
..etc..
我的 ASPX 页面引用了母版页中的 Javascripts ...
ASPX 页面
<%@ Page Language="C#" MasterPageFile="~/JobRegister/JobRegister.master" AutoEventWireup="true" CodeFile="Details.aspx.cs" Inherits="JobRegister_Details" %>
<%@ Register TagPrefix="tecnq" TagName="JobAction" Src="ActionControl.ascx" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="JobID, CreatedBy" GridLines="None"
AllowPaging="True" DefaultMode="Edit" OnDataBound="FormView1_OnDataBound" OnItemCreated="FormView1_OnItemCreated"
OnItemUpdated="FormView1_OnItemUpdated" OnItemUpdating="FormView1_Updating"
HeaderText="Job Details" CellPadding="0" Font-Names="Arial" Font-Size="10pt">
<HeaderStyle BackColor="#5D7B9D" ForeColor="White" Font-Bold="True" HorizontalAlign="Center" Height="30px" />
<InsertItemTemplate>
...
..etc..
...
<img id="imgPaste1" src="Images/imgPaste.gif" class="LinkImage" title="Click here to Paste any copied Image" onclick="javascript:pasteOnClick('1');" />
..
..etc..
..
我从一篇关于如何将剪贴板图像粘贴到 Web 表单中的精彩博客中获得了所有这些...
http://www.codeproject.com/Articles/25967/Clipboard-ActiveX-for-Image-Copy-Paste-into-Web-Fo
上面博客中的演示效果很好,但是 Javascript 和 ASPX 代码都在同一个文件中!
我怎样才能对 MasterPage 中的 Javascript 执行相同的操作并在我的 ASPx 文件中引用脚本?
我也尝试将所有代码都放在我的 ASPX 文件中,但我在这行代码中收到无效引用错误...
document.getElementById("<%=hdnImageFileName1.ClientID %>").value = "";
它说找不到对 "hdnImageFileName1" 的引用,但我知道它在我的 ASPX 页面的下方...
即
<asp:HiddenField runat="server" ID="hdnImageFileName1" />
提前致谢。
答案就像@deostroll 说的。我将我所有的 JS 代码移动到一个 custom.js 文件中,并通过标记简单地引用它。简单的。完毕!谢谢
简单问题:我如何引用母版页文件中的 JavaScript 代码(该文件与许多其他 ASPX 页面也在 同一文件夹 上),来自我的一个 ASPX 页面?
主页
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="JobRegister.master.cs" Inherits="JobRegister_Job" %>
<!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>Tec-NQ miJob Register</title>
<style type="text/css">
textarea { font-family: Arial; font-size: 10pt; }
a { white-space: nowrap; }
.tablestyle { background-color:#eeeeee; width:100%; margin-bottom:2px; padding:3px; }
.checkboxlist input { position:relative; left: 360px; }
.checkboxlist label { position:relative; left: -22px; }
.hidepanel { display: none; }
.showpanel { display: block; }
</style>
<script src="<%# ResolveUrl("~/") %>" type="text/javascript">
/*
Added : Variables required for image uploading & validation checking.
By : Amit Champaneri
On : 4th April 2008
*/
var hoverColor = "#00000b"; //DIV Color when mouse is hover the DIV
var defaultColor = "black"; //DIV default color
var selectColor = "#000000"; // DIV color when its selected.
var selectedDIV = ""; //ID of the DIV user has currently selected(it will be 1,2,3 or 4)
var objActiveX; //Object of Clipboard ActiveX control.
..
..etc..
我的 ASPX 页面引用了母版页中的 Javascripts ...
ASPX 页面
<%@ Page Language="C#" MasterPageFile="~/JobRegister/JobRegister.master" AutoEventWireup="true" CodeFile="Details.aspx.cs" Inherits="JobRegister_Details" %>
<%@ Register TagPrefix="tecnq" TagName="JobAction" Src="ActionControl.ascx" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="JobID, CreatedBy" GridLines="None"
AllowPaging="True" DefaultMode="Edit" OnDataBound="FormView1_OnDataBound" OnItemCreated="FormView1_OnItemCreated"
OnItemUpdated="FormView1_OnItemUpdated" OnItemUpdating="FormView1_Updating"
HeaderText="Job Details" CellPadding="0" Font-Names="Arial" Font-Size="10pt">
<HeaderStyle BackColor="#5D7B9D" ForeColor="White" Font-Bold="True" HorizontalAlign="Center" Height="30px" />
<InsertItemTemplate>
...
..etc..
...
<img id="imgPaste1" src="Images/imgPaste.gif" class="LinkImage" title="Click here to Paste any copied Image" onclick="javascript:pasteOnClick('1');" />
..
..etc..
..
我从一篇关于如何将剪贴板图像粘贴到 Web 表单中的精彩博客中获得了所有这些...
http://www.codeproject.com/Articles/25967/Clipboard-ActiveX-for-Image-Copy-Paste-into-Web-Fo
上面博客中的演示效果很好,但是 Javascript 和 ASPX 代码都在同一个文件中!
我怎样才能对 MasterPage 中的 Javascript 执行相同的操作并在我的 ASPx 文件中引用脚本?
我也尝试将所有代码都放在我的 ASPX 文件中,但我在这行代码中收到无效引用错误...
document.getElementById("<%=hdnImageFileName1.ClientID %>").value = "";
它说找不到对 "hdnImageFileName1" 的引用,但我知道它在我的 ASPX 页面的下方...
即
<asp:HiddenField runat="server" ID="hdnImageFileName1" />
提前致谢。
答案就像@deostroll 说的。我将我所有的 JS 代码移动到一个 custom.js 文件中,并通过标记简单地引用它。简单的。完毕!谢谢