我的 ASP.NET MVC 2 母版页上的 JavaScript 和 CSS 文件太多?

发布于 2024-09-01 12:46:57 字数 834 浏览 4 评论 0原文

我在 ASP.NET MVC 2 项目中使用 EditorTemplate DateTime.ascx

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DateTime>" %>
<%: Html.TextBox(String.Empty, Model.ToString("M/dd/yyyy h:mm tt")) %>
<script type="text/javascript">
    $(function () {
        $('#<%: ViewData.TemplateInfo.GetFullHtmlFieldId(String.Empty) %>').AnyTime_picker({
            format: "%c/%d/%Y %l:%i %p"
        });
    });
</script>

它使用 Andrew M. Andrews III 的 Any+Time™ JavaScript 库。

我已将这些库文件(anytimec.jsanytimec.css)添加到母版页的 部分。

与其在网站的每个页面上包含这些 JavaScript 和级联样式表文件,不如如何仅在需要它们的页面上包含 .js 和 .css 文件——编辑日期时间的页面输入值?

I'm using an EditorTemplate DateTime.ascx in my ASP.NET MVC 2 project.

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DateTime>" %>
<%: Html.TextBox(String.Empty, Model.ToString("M/dd/yyyy h:mm tt")) %>
<script type="text/javascript">
    $(function () {
        $('#<%: ViewData.TemplateInfo.GetFullHtmlFieldId(String.Empty) %>').AnyTime_picker({
            format: "%c/%d/%Y %l:%i %p"
        });
    });
</script>

This uses the Any+Time™ JavaScript library for jQuery by Andrew M. Andrews III.

I've added those library files (anytimec.js and anytimec.css) to the <head> section of my master page.

Rather than include these JavaScript and Cascading Style Sheet files on every page of my web site, how can I instead include the .js and .css files only on pages that need them--pages that edit a DateTime type value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

家住魔仙堡 2024-09-08 12:46:57

我想到的第一个想法=>

模板:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DateTime>" %>
<%: Html.TextBox(String.Empty, Model.ToString("M/dd/yyyy h:mm tt")) %>
<script type="text/javascript">
    $(function () {
        MakeSureAnyTimeIsIncluded();
        $('#<%: ViewData.TemplateInfo.GetFullHtmlFieldId(String.Empty) %>').AnyTime_picker({
            format: "%c/%d/%Y %l:%i %p"
        });
    });
</script>

母版页或共享外部 JS 文件:

  function MakeSureAnyTimeIsIncluded(){
    if (!anyTimeIsIncluded)
      //document.write(<script src="correct url") something like that
      anyTimeIsIncluded=true;
  }

First idea that comes to mind =>

Template:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DateTime>" %>
<%: Html.TextBox(String.Empty, Model.ToString("M/dd/yyyy h:mm tt")) %>
<script type="text/javascript">
    $(function () {
        MakeSureAnyTimeIsIncluded();
        $('#<%: ViewData.TemplateInfo.GetFullHtmlFieldId(String.Empty) %>').AnyTime_picker({
            format: "%c/%d/%Y %l:%i %p"
        });
    });
</script>

masterpage or shared external JS file:

  function MakeSureAnyTimeIsIncluded(){
    if (!anyTimeIsIncluded)
      //document.write(<script src="correct url") something like that
      anyTimeIsIncluded=true;
  }
流云如水 2024-09-08 12:46:57

在您的主控中:

<asp:ContentPlaceHolder ID="Scripts" runat="server" />

以及在将使用 EditorTemplate/插件的视图 (aspx) 中:

<asp:Content ID="indexScripts" ContentPlaceHolderID="Scripts" runat="server">
    <script type="text/javascript" src="anytime.js"></script>
</asp:Content>

In your master:

<asp:ContentPlaceHolder ID="Scripts" runat="server" />

And in the Views (aspx) that will use the EditorTemplate/plugin:

<asp:Content ID="indexScripts" ContentPlaceHolderID="Scripts" runat="server">
    <script type="text/javascript" src="anytime.js"></script>
</asp:Content>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文