2017-01-22 3 views
0

が_Layout.cshtmlに使用できる子ページに基づいて、マスターページ要素をレンダリング:@Section stylesと一緒にはMVC 1では

@if (IsSectionDefined("styles")) 
{@RenderSection("styles", required: false)} 

をコンテンツページ/レイアウトで指定されたセクションが必要な場合<head>内のCSSスタイルを含めること。

Webフォームで同じ動作を模倣する効率的な方法はありますか?

要約:マスターページに基づいて特定の子ページで必要な場合のみ、マスターページでCSSとjsをレンダリングすることができます。したがって、これらのcssとjsファイルを必要としないページには含まれません。

答えて

1

ContentPlaceHolderを使用できます。次のようにマスターページでは、コンテンツプレースホルダを定義します。

<head runat="server"> 
    <title>..</title> 
    <meta charset="utf-8"/> 
    .. 
    <asp:ContentPlaceHolder ID="head" runat="server"> 
    </asp:ContentPlaceHolder> 
</head> 

..and例えば自分の必要なファイルを、注入するために、このコンテンツプレースホルダを使用することができ、特別なcssまたはjsファイルを必要とする児童のページ:

<%@ Page Title="" Language="C#" MasterPageFile=".." AutoEventWireup="true" CodeBehind=".." %> 
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"> 
    <link href="some css file dedicated only to this child page" rel="stylesheet" /> 
    <script src="..js.."></script> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> 
    .. 
    <h1><asp:Label ID="lblPageTitle" runat="server" Text=""></asp:Label></h1> 
    .. 
</asp:Content> 
関連する問題