无法将Uploadify用于使用母版页的ASP.NET

时间:2017-05-24 21:44:19

标签: javascript c# jquery asp.net

以下代码适用于独立的Web表单页面,但在我将其带入具有母版页的网站时则不行。使用Chrome开发人员工具,我看到jQuery和jQuery.uploadify已加载。页面加载时发生的错误是:

  

未捕获的TypeError:$(...)。uploadify不是函数

在线发生错误:

$("#file_upload").uploadify({

以下是代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/Shared/DefaultMaster.master" AutoEventWireup="true" CodeBehind="UploadUploadify.aspx.cs" Inherits="Impact.Thm.Web.Utilities.UploadUploadify" %>
<asp:Content ID="Content1" ContentPlaceHolderID="DefaultHeaderContent" runat="server">
    <link rel="Stylesheet" type="text/css" href="./App_Themes/Uploader/uploadify.css" />
    <script type="text/javascript" src="scripts/jquery-1.8.3.js"></script>
    <script type="text/javascript" src="./Scripts/jquery.uploadify.min.js"></script>
    <title>Testing Uploadify</title>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="JavascriptContent" runat="server">
    <script type = "text/javascript">
        $(function () {
            $("#file_upload").uploadify({
                'auto': false,
                'swf': '/Scripts/uploadify.swf',
                'uploader': 'UploadifyStatus.ashx'
            });
        });
    </script>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="DefaultContent" runat="server">
        <div style="border: thin solid black">
            <p><input type="file" name="file_upload" id="file_upload" /></p>
        </div>
        <p><a href="javascript:$('#file_upload').uploadify('upload','*')">Upload Files</a></p>
</asp:Content>

如何摆脱此错误,以便它看到uploadify函数?

2 个答案:

答案 0 :(得分:0)

我建议您将点击事件移至页面加载事件(使用其他uploadify函数),而不是链接本身:

<asp:Content ID="Content2" ContentPlaceHolderID="JavascriptContent" runat="server">
    <script type="text/javascript">
        $(function() {
            $("#file_upload").uploadify({
                'auto': false,
                'swf': '/Scripts/uploadify.swf',
                'uploader': 'UploadifyStatus.ashx'
            });

            $("#upload-link").click(function() {
                $('#file_upload').uploadify('upload','*');
            });

        });
    </script>
</asp:Content>


<asp:Content ID="Content3" ContentPlaceHolderID="DefaultContent" runat="server">
    <div style="border: thin solid black">
        <p><input type="file" name="file_upload" id="file_upload" /></p>
    </div>
    <p>
        <a id="upload-link" href="#">Upload Files</a>
    </p>
</asp:Content>

答案 1 :(得分:0)

要在使用母版页时让jQuery.uploadify正常工作,我必须添加对javascript文件的引用。将以下内容添加到DefaultMaster.master

<asp:ScriptManager> ... 
    <Scripts>
        <asp:ScriptReference Path="~/Scripts/jquery.uploadify.min.js" />

现在我没有收到错误:

Uncaught TypeError: $(...).uploadify is not a function