创建自定义WebControl
, UserControl
,Control
并且需要覆盖各种 Render 方法时,主要有两种方法可以完成此操作:
又快又脏:
public override void RenderControl(HtmlTextWriter writer)
{
writer.Write("<div class=\"{0}\"><a href={1}>{2}</a>", CssClass, "/someurl.aspx", LocalizedStrings.EditLinkText);
base.RenderControl(writer);
writer.Write("</div>");
}
又长又干净:
public override void RenderControl(HtmlTextWriter writer)
{
writer.WriteBeginTag(HtmlTextWriterTag.Div.ToString());
writer.WriteAttribute(HtmlTextWriterAttribute.Class.ToString(), CssClass);
writer.Write(HtmlTextWriter.TagRightChar);
var link = new HtmlAnchor();
link.HRef = "/someurl.aspx";
link.Title = LocalizedStrings.EditLinkTitle;
link.InnerText = LocalizedStrings.EditLinkText; ;
link.Attributes.Add(HtmlTextWriterAttribute.Class.ToString(), "someclass");
link.RenderControl(writer);
base.RenderControl(writer);
writer.WriteEndTag(HtmlTextWriterTag.Div.ToString());
}
我想知道第二种方法是否值得。