繁体   English   中英

如何从另一个ASPX页面内引用MasterPage中的JavaScript代码

[英]How to reference JavaScript code in MasterPage from within another aspx page

一个简单的问题:如何从我的一个ASPX页面中引用一个母版页文件(与许多其他ASPX页位于同一文件夹中)上的JavaScript代码?

母版页

    <%@ 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页面对母版页中的Java语言有引用。

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文件中,并仅通过标记对其进行了引用。 简单。 做完了! 谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM