Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
522 views
in Technique[技术] by (71.8m points)

header - ASP.NET masterpages: how to insert markup in the head section inside the aspx?

I know I can access the head section of a page which uses a masterpage programmatically this way (in code behind):

This is only an example (I'd like to insert scripts and styles etc.):

this.Header.Title = "I just set the page's title";

Is there a simple way to do this in a declarative way on in the aspx file itself?

Sometimes it would be handy to insert a client script or a style declaration or a link to an external resource.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do this by using content regions in the head, in exactly the same way as you would in the body of the page. eg, In your masterpage:

<head>
    <link type="text/css" rel="stylesheet" href="/styles/common1.css" />
    <script type="text/javascript" src="/scripts/common1.js"></script>
    <asp:contentplaceholder id="ExtraStylesAndScripts" runat="server" />
</head>

And then in the page itself just something like:

<asp:content contentplaceholderid="ExtraStylesAndScripts" runat="server">    
    <link type="text/css" rel="stylesheet" href="/styles/extra1.css" />
    <link type="text/css" rel="stylesheet" href="/styles/extra2.css" />
    <script type="text/javascript" src="/scripts/extra1.js"></script>
    <script type="text/javascript" src="/scripts/extra2.js"></script>
</asp:content>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...