How to correctly register javascript blocks in an ASP.NET MVC 2 (RTM) editor template?
The specific scenario I'm participating in is that I want to use Dynarch JSCal2 DateTimePicker for my standard datetime collector, but this question is generally for any reusable javascript package. Now I have my template, but I have my JS and CSS on my main page, and I would prefer to include these things only if I really need them:
<link rel="stylesheet" type="text/css" href="../../Content/JSCal2-1.7/jscal2.css" /> <link rel="stylesheet" type="text/css" href="../../Content/JSCal2-1.7/border-radius.css" /> <script type="text/javascript" src="../../Scripts/JSCal2-1.7/jscal2.js"></script> <script type="text/javascript" src="../../Scripts/JSCal2-1.7/lang/en.js"></script>
So, obviously, I could just put these lines in my template, but if I have a screen with 5 DateTimePickers, then this content will be duplicated 5 times, which would not be ideal. In any case, I still want my view template to run this code in the <head> my page.
As long as I am completely unconnected with asking this question, I thought I would share my template here (for now) if it would be useful anyway:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DateTime>" %> <%= Html.TextBoxFor(model => Model) %> <input type="button" id="<%= ViewData.TemplateInfo.GetFullHtmlFieldId("cal-trigger") %>" value="..." /> <script type="text/javascript"> var <%= ViewData.TemplateInfo.GetFullHtmlFieldId("cal") %> = Calendar.setup({ trigger : "<%= ViewData.TemplateInfo.GetFullHtmlFieldId(string.Empty) %>", inputField : "<%= ViewData.TemplateInfo.GetFullHtmlFieldId(string.Empty) %>", onSelect : function() { this.hide(); }, showTime : 12, selectionType : Calendar.SEL_SINGLE, dateFormat : '%o/%e/%Y %l:%M %P' }); </script>
javascript asp.net-mvc asp.net-mvc-2 mvc-editor-templates
Jaxidian
source share