简体   繁体   English

ASP.NET MVC中的CSS和JavaScript 2页

[英]CSS and JavaScript in ASP.NET MVC 2 Pages

I have a master page that has two content sections like this (left some parts out): 我有一个母版页,其中包含两个这样的内容部分(省略了一些部分):

<head runat="server">
    <link href="/Content/Site.css" rel="stylesheet" type="text/css" />
    <asp:ContentPlaceHolder runat="server" ID="HeadContent" />
</head>

<body>
    <div id="content">
        <div id="main">
            <asp:ContentPlaceHolder ID="MainContent" runat="server" />
        </div>
    </div>
</body>

In one of my .aspx pages which references this master page, I supply the content like this (left some parts out): 在引用此母版页的.aspx页之一中,我提供了以下内容(省略了某些部分):

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

    <% using (Html.BeginForm("Create", "Document", FormMethod.Post))
       { %>

       <p>
           <%: Html.LabelFor(m => m.Title) %>
           <%: Html.TextBoxFor(m => m.Title) %>
           <%: Html.ValidationMessageFor(m => m.Title) %>
       </p>

       <p>
            <input type="submit" class="t-button t-state-default" value="Create" />
       </p>

    <% } %>


</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>

Yet the label created by the LabelFor chunk above does not get styled. 但是,上面的LabelFor块创建的标签未设置样式。 In my CSS file, I have a rule to set the font globally and this is not applying - the font is defaulting instead. 在我的CSS文件中,我有一条规则来全局设置字体,而这并不适用-而是默认字体。

Do I need to re-include all my CSS and Javascript in the 'Content2' section in the .aspx page? 我需要在.aspx页的“ Content2”部分中重新包含所有CSS和Javascript吗? Isn't it sufficient to include them once in Site.Master? 将它们一次包含在Site.Master中是否足够?

Have you included a stylesheet rule for <label> ? 您是否包含<label>的样式表规则?

Use a DOM inspector (Firebug, etc) to see where the label style (or lack thereof) is coming from. 使用DOM检查器(Firebug等)查看标签样式(或缺少标签样式)的来源。

我必须为<label>定义样式表规则,如下所示:

label { margin:10px; color:blue; }

You only need to include CSS and JavaScript references in the master page. 您只需要在母版页中包含CSS和JavaScript引用即可。 The markup from both the master page and content pages are combined and sent as a single HTML document. 来自母版页和内容页的标记被合并并作为单个HTML文档发送。

Your CSS probably doesn't match your markup properly so the styles aren't being applied correctly. 您的CSS可能与标记不正确匹配,因此样式未正确应用。 You could add the CSS to your question for more assistance or use Firebug to inspect the source and styles. 您可以在问题中添加CSS以获得更多帮助,也可以使用Firebug检查源和样式。

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

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