【问题标题】:How to get functions from a javascript working in c# .aspx file, pointing to the right folder/file with href and src如何从在 c# .aspx 文件中工作的 javascript 中获取函数,并使用 href 和 src 指向正确的文件夹/文件
【发布时间】:2012-01-05 09:09:25
【问题描述】:

我有一个 .aspx 文件,我想在其中拥有一个从 here 获得的文件上传器。有一些例子包括如何让它工作。当我测试它只做 HTML 和 javascript 时,我可以让它工作,但是当我尝试让它与 C# 一起工作时,我无法正确获取 .js 文件的路径。

示例 html/javascript 文件为:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link href="fileuploader.css" rel="stylesheet" type="text/css"> 
    <style>     
        body {font-size:13px; font-family:arial, sans-serif; width:700px; margin:100px auto;}
    </style>    
</head>    
    <script src="fileuploader.js" type="text/javascript"></script>
    <script>        
        function createUploader(){            
            var uploader = new qq.FileUploader({
                element: document.getElementById('file-uploader-demo1'),
                action: 'do-nothing.htm',
                debug: true
            });           
        }

        // in your app create uploader as soon as the DOM is ready
        // don't wait for the window to load  
        window.onload = createUploader;     
    </script>    
</body>
</html>

我想在下面的 .aspx 文件中得到它。 .js 和 .css 文件位于 D:\svn\Web\Framework\Trunk\test.Web.Framework\Scripts\fileuploader 位置,.aspx 文件位于 D:\svn\Web\Healthcare\trunk\test .Web.Healthcare\Areas\Framework\Administration\Entity。我尝试执行以下操作,但这给了我一个 GET =1325724928825">http://localhost:1304/Administration/blue/en-gb/Entity/Index/~/Scripts/fileuploader/fileuploader.js?=1325724928825 404 (未找到)错误:

<%@ Control Language="C#" Inherits="test.Web.Framework.Core.ViewUserControl<test.Web.Framework.Areas.Administration.Models.NoteModel>" %>
<% using (UI.koform(Model, null))
{ %>
    <div class="ui-state-highlight ui-corner-all highlight" data-bind="visible: meta.message">
        <span class="ui-icon ui-icon-info"></span><strong data-bind="text: meta.message">
        </strong>
    </div>

    Subject:
    <input type="text" data-bind="value:subject" />
    <span data-bind="text: subject"></span>
    <br />
    Text:
    <input type="text" data-bind="value:text" />
    <br />
    <!--
    <a href="#" data-bind="click:function(){setvalues() }">set values</a>

-->

    <div class="dialogButtons">
        <button onclick="$('#<%:Model.meta.modelname%>').koform('submit');">
            Save</button>
    </div>


    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link href="~/Scripts/fileuploader/fileuploader.css" rel="stylesheet" type="text/css">  
    </head>
    <div id="file-uploader-demo1">      
        <noscript>          
            <p>Please enable JavaScript to use file uploader.</p>
            <!-- or put a simple form for upload here -->
        </noscript>         
    </div>

    "~/Scripts/fileuploader/fileuploader.css"
    <script src="~/Scripts/fileuploader/fileuploader.js" type="text/javascript"></script>
    <script>        
            function createUploader() {
                var uploader = new qq.FileUploader({
                    element: document.getElementById('file-uploader-demo1'),
                    action: 'do-nothing.htm',
                    debug: true
                });
            }

            // in your app create uploader as soon as the DOM is ready
            // don't wait for the window to load  
            window.onload = createUploader;     
        </script>    

<%}%>

【问题讨论】:

    标签: c# javascript asp.net src


    【解决方案1】:

    轻松修复。用这样的东西替换你的hrefs...

    <script src="<%=ResolveClientUrl("~/Scripts/fileuploader/fileuploader.js")%>" type="text/javascript"></script>
    

    这将解析相对于您的 Web 应用程序根文件夹的 URL。对您的样式表和其他引用执行相同的操作。

    我在所有地方都使用它,尤其是在母版页中。

    【讨论】:

    • 太好了,你也知道剃须刀怎么做吗?
    • 剃须刀@Url.Content("~/Scripts/fileuploader/fileuploader.js")
    猜你喜欢
    • 1970-01-01
    • 2012-08-14
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多