Try something like this if you are using WebForms views ( not verified ):
using System.Web.UI.HtmlControls; public static string Calendar(this HtmlHelper html, HtmlHead head) { var urlHelper = new UrlHelper(html.ViewContext.RequestContext); var url = urlHelper.Content("~/Script/calendar.js"); //var scriptControl = new HtmlGenericControl("script"); //scriptControl.Attributes.Add("src", url); //scriptControl.Attributes.Add("type", "text/javascript"); //if(head.Controls.Contains(scriptControl)) //{ // head.Controls.Add(scriptControl); //} // or if(!head.Controls.Cast<Control>().Any(x => (x as HtmlGenericControl) != null && (x as HtmlGenericControl).Attributes["src"] == url)) { var scriptControl = new HtmlGenericControl("script"); scriptControl.Attributes.Add("src", url); scriptControl.Attributes.Add("type", "text/javascript"); head.Controls.Add(scriptControl); } return "<input type='text' onclick='showCalendar(this)'/>"; }
In view:
<%= Html.Calendar(Header) %>
Hope this works :)
source share