/**//// <summary>
/// 作者 邹健
/// 日期 20070202
/// 重载的Page类。
/// </summary>
public class ChPage : Page {
/**//// <summary>
/// 构造函数。
/// </summary>
public ChPage() { }
/**//// <summary>
/// Render函数。
/// </summary>
/// <param name="writer">HtmlTextWriter。</param>
protected override void Render(HtmlTextWriter writer) {
if (writer is System.Web.UI.Html32TextWriter) {
writer = new FormFixerHtml32TextWriter(writer.InnerWriter);
}
else {
writer = new FormFixerHtmlTextWriter(writer.InnerWriter);
}
base.Render(writer);
}
/**//// <summary>
/// 设置Html标签内,的Link标签,如Css
/// </summary>
/// <param name="cssfile">Css文件。</param>
protected void SetHtmlLink(string cssfile) {
HtmlLink myHtmlLink = new HtmlLink();
myHtmlLink.Href = cssfile;
myHtmlLink.Attributes.Add("rel", "stylesheet");
myHtmlLink.Attributes.Add("type", "text/css");
Page.Header.Controls.Add(myHtmlLink);
}
/**//// <summary>
/// 该函数可获得web.config中的字符串。
/// </summary>
/// <param name="Str">指定项的键值。</param>
/// <returns>返回键值所指的值。</returns>
protected string GetString(string Str) {
return System.Configuration.ConfigurationManager.AppSettings[Str];
}
}
/// 作者 邹健
/// 日期 20070202
/// 重载的Page类。
/// </summary>
public class ChPage : Page {
/**//// <summary>
/// 构造函数。
/// </summary>
public ChPage() { }
/**//// <summary>
/// Render函数。
/// </summary>
/// <param name="writer">HtmlTextWriter。</param>
protected override void Render(HtmlTextWriter writer) {
if (writer is System.Web.UI.Html32TextWriter) {
writer = new FormFixerHtml32TextWriter(writer.InnerWriter);
}
else {
writer = new FormFixerHtmlTextWriter(writer.InnerWriter);
}
base.Render(writer);
}
/**//// <summary>
/// 设置Html标签内,的Link标签,如Css
/// </summary>
/// <param name="cssfile">Css文件。</param>
protected void SetHtmlLink(string cssfile) {
HtmlLink myHtmlLink = new HtmlLink();
myHtmlLink.Href = cssfile;
myHtmlLink.Attributes.Add("rel", "stylesheet");
myHtmlLink.Attributes.Add("type", "text/css");
Page.Header.Controls.Add(myHtmlLink);
}
/**//// <summary>
/// 该函数可获得web.config中的字符串。
/// </summary>
/// <param name="Str">指定项的键值。</param>
/// <returns>返回键值所指的值。</returns>
protected string GetString(string Str) {
return System.Configuration.ConfigurationManager.AppSettings[Str];
}
}
但是后来发现在使用asp.net编程时,如果应用模板页的话,不止是css文件不容易后往里添,javascript文件也是如此,虽然说在模板页的<head />中建一个contentplaceholder也算可以,但那种方法毕竟不太雅,有失风范。于是就想按这种添加<link />标签的方法来泡制<script />
但发现在System.Web.UI.HtmlControls中有HtmlLink来表现<link />但却没有表现<Script />标签的类。
没办法了,因为懒所以只能自己写一个了,代码不长,如下。
namespace Chsword.Class {
/// <summary>
/// 可以生成script标签
/// </summary>
[ControlBuilder(typeof(HtmlEmptyTagControlBuilder)), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class HtmlScript : HtmlControl {
// Methods
public HtmlScript()
: base("script") {
}
protected override void Render(HtmlTextWriter writer) {
writer.WriteBeginTag(this.TagName);
this.RenderAttributes(writer);
writer.Write(">");
writer.WriteEndTag(TagName);
}
protected override void RenderAttributes(HtmlTextWriter writer) {
if (!string.IsNullOrEmpty(this.Src)) {
base.Attributes["src"] = base.ResolveClientUrl(this.Src);
}
base.Attributes["type"] = "text/javascript";
base.RenderAttributes(writer);
}
/// <summary>
/// 脚本的URL
/// </summary>
[UrlProperty]
[DefaultValue("")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public virtual string Src {
get {
string text = base.Attributes["src"];
if (text == null) {
return string.Empty;
}
return text;
}
set {
base.Attributes["src"] = MapStringAttributeToString(value);
}
}
string MapStringAttributeToString(string s) {
if ((s != null) && (s.Length == 0)) {
return null;
}
return s;
}
}
}
/// <summary>
/// 可以生成script标签
/// </summary>
[ControlBuilder(typeof(HtmlEmptyTagControlBuilder)), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class HtmlScript : HtmlControl {
// Methods
public HtmlScript()
: base("script") {
}
protected override void Render(HtmlTextWriter writer) {
writer.WriteBeginTag(this.TagName);
this.RenderAttributes(writer);
writer.Write(">");
writer.WriteEndTag(TagName);
}
protected override void RenderAttributes(HtmlTextWriter writer) {
if (!string.IsNullOrEmpty(this.Src)) {
base.Attributes["src"] = base.ResolveClientUrl(this.Src);
}
base.Attributes["type"] = "text/javascript";
base.RenderAttributes(writer);
}
/// <summary>
/// 脚本的URL
/// </summary>
[UrlProperty]
[DefaultValue("")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public virtual string Src {
get {
string text = base.Attributes["src"];
if (text == null) {
return string.Empty;
}
return text;
}
set {
base.Attributes["src"] = MapStringAttributeToString(value);
}
}
string MapStringAttributeToString(string s) {
if ((s != null) && (s.Length == 0)) {
return null;
}
return s;
}
}
}
/// <summary>
/// 设置script现在只能是type=text/javsscript的
/// </summary>
/// <param name="src">脚本地址</param>
protected void SetHtmlScript(string src) {
HtmlScript myHtmlScript = new HtmlScript();
myHtmlScript.Src = src;
Page.Header.Controls.Add(myHtmlScript);
}
/// 设置script现在只能是type=text/javsscript的
/// </summary>
/// <param name="src">脚本地址</param>
protected void SetHtmlScript(string src) {
HtmlScript myHtmlScript = new HtmlScript();
myHtmlScript.Src = src;
Page.Header.Controls.Add(myHtmlScript);
}
使用方法
原来的页面脚本是这样的
vb.net
Partial Class DefaultClass Default
Inherits Page
Inherits Page
现在只要改成
Partial Class DefaultClass Default
Inherits ChPage
Inherits ChPage
就可以了
使用时只要在载入前的某个事件,比如Init或Load写 SetHtmlScript("/Javascript/MicrosoftAjax.js")
这样就OK了
直接再看看生成的HTML,吼吼,已经有你添加的<script src=""/Javascript/MicrosoftAjax.js" type="text/javascript"></script>了
本如果文章中有不足的地方请大家要提出啊
文章来自chsword[邹健的博客]允许转载麻烦留言
邹健 2007-08-01 12:47 发表评论
文章来源:http://www.cnblogs.com/chsword/archive/2007/08/01/838870.html