【问题标题】:How to add jQuery UI code to ASP.NET Web Forms pages?如何将 jQuery UI 代码添加到 ASP.NET Web 窗体页面?
【发布时间】:2013-11-07 16:56:57
【问题描述】:

当您在 ASP.NET Web 窗体项目中选择“新建”>“Web 窗体”时,您将获得一个包含以下代码的页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DuckbilledPlatypus.aspx.cs" 
Inherits="DuckbilledPlatypus" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    </div>
    </form>
</body>
</html>

毫无疑问,添加 jQuery 代码的方式与典型方式相同(例如,在 Razor / Web Pages 应用程序/站点中),即引用必要的 jQuery 和 jQueryUI *.js 和 *.css 文件,然后添加 jQuery在脚本元素中。

但是,提供的表单/页面(“关于”和“联系方式”)有这样的代码:

<%@ Page Title="About" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" 
CodeFile="About.aspx.cs" Inherits="About" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2><%: Title %>.</h2>
    <h3>Your application description page.</h3>
    <p>Use this area to provide additional information.</p>
</asp:Content>

jQuery/jQueryUI 代码是如何添加到其中的?可以简单地将 *.js 和 *.css 引用以及脚本部分添加到 asp:Content 封装中吗?

【问题讨论】:

    标签: jquery html asp.net webforms visual-studio-2013


    【解决方案1】:

    您的第二个 sn-p 正在使用母版页。您可以在 asp:content 标记中添加脚本引用,但将引用添加到母版页可能会更好/更简单。

    【讨论】:

    • 即使我只在一页上使用jQuery/jQueryUI?
    • 如果您真的只在一个页面上使用它,那么您可以在该页面的内容标签中引用脚本。请注意,在一个页面上多次包含对 jquery 的引用可能会导致问题,因此如果您稍后决定将其添加到母版页,它可能会停止在此页面上工作,直到您从该页面中删除引用。
    【解决方案2】:

    这就是我的工作方式..

    <script src="Scripts/jquery-ui-1.10.3.min.js"></script>
    <script src="Plugins/jquery.cookie.js"></script>
    <link href="Content/themes/Smoothness/jquery-ui-1.10.3.custom.css" rel="stylesheet" type="text/css" />
    <script>
        /**
          *This script creates a cookie that expires every 7 days. If you don't have this cookie
          *then the popup shows. If the cookie isn't expired, the popup doesn't show. 
         **/
        $(document).ready(function () {
            if ($.cookie('modal_shown') == null) {
                $.cookie('modal_shown', 'yes', { expires: 7, path: '/' });
                $('#popup').dialog({
                    modal: true,
                    buttons: {
                        Ok: function () {
                            $(this).dialog("close");
                        }
                    }
                });
            }
        });
    </script>
    <div id="popup" style="display: none" title="New Release!">
        <span class="ui-icon-alert" style="float: left; margin: 0 7px 50px 0;"></span>
        <p><b>Issues Resolved</b></p>
        <ul>
            <li>New thing 1</li>
            <li>New thing 2</li>
            <li>New thing 3</li>
        </ul>
    </div>
    <h2><%: Title %>.</h2>
    <h3>Your application description page.</h3>
    <p>Use this area to provide additional information.</p>
    

    这是使用母版页的页面。

    对于不使用母版页的 Web 表单,您可以这样做......

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DuckbilledPlatypus.aspx.cs" 
    Inherits="DuckbilledPlatypus" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <script src="Scripts/jquery-2.0.2.min.js"></script>
        <script src="Scripts/jquery-ui-1.10.3.min.js"></script>
        <script src="Plugins/jquery.cookie.js"></script>
        <link href="Content/themes/Smoothness/jquery-ui-1.10.3.custom.css" rel="stylesheet" type="text/css" />
        <script>
            /**
              *This script creates a cookie that expires every 7 days. If you don't have this cookie
              *then the popup shows. If the cookie isn't expired, the popup doesn't show. 
             **/
            $(document).ready(function () {
                if ($.cookie('modal_shown') == null) {
                    $.cookie('modal_shown', 'yes', { expires: 7, path: '/' });
                    $('#popup').dialog({
                        modal: true,
                        buttons: {
                            Ok: function () {
                                $(this).dialog("close");
                            }
                        }
                    });
                }
            });
        </script>
        <div id="popup" style="display: none" title="New Release!">
            <span class="ui-icon-alert" style="float: left; margin: 0 7px 50px 0;"></span>
            <p><b>Issues Resolved</b></p>
            <ul>
                <li>New thing 1</li>
                <li>New thing 2</li>
                <li>New thing 3</li>
            </ul>
        </div>
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
    
        </div>
        </form>
    </body>
    </html>
    

    如您所知,您只需将其放在标签中即可。我希望这会有所帮助!

    【讨论】:

    • 最好将script放在页面底部,这是一个很好的做法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-26
    • 2014-05-29
    • 1970-01-01
    • 2012-01-04
    • 2018-07-05
    • 1970-01-01
    • 2010-11-13
    相关资源
    最近更新 更多