自定义服务端Script控件支持javascript服务端路径?
[DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), DefaultProperty("Text"), AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)] public class Script : Control { public string Type { get; set; } public string Src { get; set; } protected override void AddParsedSubObject(object obj) { if (!(obj is Script)) { throw new HttpException(string.Format("Script cannot Have Children Of Type {0}.", obj.GetType().Name.ToString(CultureInfo.InvariantCulture))); } this.Type = ((Script)obj).Type; this.Src = ((Script)obj).Src; } protected override ControlCollection CreateControlCollection() { return new EmptyControlCollection(this); } [EditorBrowsable(EditorBrowsableState.Never)] public override void Focus() { throw new NotSupportedException(string.Format("NoFocusSupport of type {0}.", base.GetType().Name)); } protected override void Render(HtmlTextWriter writer) { string url = string.Empty; try { url = this.Page.ResolveUrl(this.Src??string.Empty); } catch (Exception) { } string text = string.Format("<script type=\"{0}\" src=\"{1}\"></script>", this.Type ?? string.Empty, url); writer.Write(text); } }
在aspx页面中的使用方法:
<%@ Register Assembly="ContentManagement.Entity" Namespace="ContentManagement.Entity.Aspx" tagprefix="aspx" %> <aspx:Script runat="server" Type="text/javascript" Src="~/Scripts/jquery-1.4.2.min.js"></aspx:Script> <aspx:Script runat="server" Type="text/javascript" Src="~/Scripts/jquery.validate.js"></aspx:Script> <aspx:Script runat="server" Type="text/javascript" Src="~/Scripts/jquery.boxy.js"></aspx:Script>
此乃照猫画虎,通过反编译Litral控件写出来的,不对之处还请指正。
桂棹兮兰桨,击空明兮溯流光。