Project DescriptionMVC ContentMarker is an easy way to write to different parts of a page with ASP.NET MVC such as adding tags to the header or scripts to the bottom of the page. You can even create custom sections to write to!
Here is an example! Imagine you have a page like this and you want to add a script and a stylesheet --
but you want to be able to do it from a partial control named SomeControl.
Start by adding
ContentMarkers at each point you want to write to on the page...
[Index.aspx]
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<html>
<head runat="server">
<title>Just A Test</title>
<% // A marker to add items to the header %>
<% this.Html.InsertMarker(Document.Head); %>
</head>
<body>
<h1>Welcome To The Page</h1>
<% this.Html.RenderPartial("SomeControl"); %>
</body>
<% // A marker to add scripts to the end of the page %>
<% this.Html.InsertMarker(Document.Scripts); %>
</html>
... Then you can write to the ContentMarkers with just a couple lines of code!
[SomeControl.ascx]
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<h3>This is the content for SomeControl</h3>
<% // include a style sheet
this.Html.AppendToMarker(
Document.Head,
@"<link rel=""stylesheet"" type=""text/css"" href=""style.css"" />"
); %>
<% // add an alert box to the page
this.Html.AppendToMarker(
Document.Scripts,
@"<script type=""text/javascript"" >alert('Howdy!');</script>"
); %>
... And now you have a page with your included content!
[Rendered Output]
<html>
<head runat="server">
<title>Just A Test</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<h1>Welcome To The Page</h1>
<h3>This is the content for SomeControl</h3>
</body>
<script type="text/javascript" >alert('Howdy!');</script>
</html>
It's just that simple!
More At My Blog!
You can read more about this project by visiting my blog at
http://somewebguy.wordpress.com/2009/07/28/include-stylesheets-and-scripts-from-a-webcontrol-in-mvc/.
.