自定义控件(4)HtmlTextWriter的三个最佳拍档


上篇文章深入的学习了HtmlTextWriter,这篇文章我来讲讲HtmlTextWriter的三个最佳拍档。HtmlTextWriterTag,HtmlTextWriterAttribute,HtmlTextWriterStyle.

1.HtmlTextWriterTag用来表示HTML标签,使用HtmlTextWriter与HtmlTextWriterTag我们可以很方便定义html,下面我们创建一个div

    StringWriter sw = new System.IO.StringWriter();
    HtmlTextWriter writer = new HtmlTextWriter(sw);
    writer.RenderBeginTag(HtmlTextWriterTag.Div);     //<div>
    writer.RenderEndTag();                            //</div>
    pre1.InnerText = sw.ToString();


2.HtmlTextWriterAttribute用来表示HTML标签上的属性,需要先定义属性,再定义标签

        StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(sw);
writer.AddAttribute(HtmlTextWriterAttribute.Width,"100px");
writer.AddAttribute(HtmlTextWriterAttribute.Height, "100px");
writer.RenderBeginTag(HtmlTextWriterTag.Img); //<div>
writer.RenderEndTag(); //</div>
pre1.InnerText = sw.ToString();
Response.Write(sw.ToString());


也可以不使用HtmlTextWriterAttribute

    StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(sw);
writer.AddAttribute("Width","100px");
writer.AddAttribute("Height", "100px");
writer.RenderBeginTag(HtmlTextWriterTag.Img); //<div>
writer.RenderEndTag(); //</div>
pre1.InnerText = sw.ToString();
Response.Write(sw.ToString());

显示结果


3.HtmlTextWriterStyle用来表示样式:要先定义样式属性再定义标签

 

        StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(sw);
writer.AddStyleAttribute(HtmlTextWriterStyle.Width,"100px");
writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "100px");
writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundColor, "red");
writer.RenderBeginTag(HtmlTextWriterTag.Div); //<div>
writer.RenderEndTag(); //</div>
pre1.InnerText = sw.ToString();
Response.Write(sw.ToString());

也可以不使用HtmlTextWriterStyle

        StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(sw);
writer.AddStyleAttribute("width","100px");
writer.AddStyleAttribute("Height", "100px");
writer.AddStyleAttribute("background-color", "red");
writer.RenderBeginTag(HtmlTextWriterTag.Div); //<div>
writer.RenderEndTag(); //</div>
pre1.InnerText = sw.ToString();
Response.Write(sw.ToString());

显示结果


下面展示一个综合实例:

       

        StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(sw);
//最外出DIV的样式属性
writer.AddStyleAttribute(HtmlTextWriterStyle.TextAlign,"center");
writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "194px");
writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "194px");
writer.AddStyleAttribute("background", "url(images/background.gif) no-repeat left");

//最外层的Div开始
writer.RenderBeginTag(HtmlTextWriterTag.Div);

//Img标签的属性和样式属性
writer.AddAttribute(HtmlTextWriterAttribute.Src,"images/nature.jpg");
writer.AddAttribute(HtmlTextWriterAttribute.Width,"160");
writer.AddAttribute(HtmlTextWriterAttribute.Height, "160");
writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, "none");
writer.AddStyleAttribute(HtmlTextWriterStyle.Padding, "0px");
writer.AddStyleAttribute(HtmlTextWriterStyle.MarginTop, "16px");

//生成Img标签
writer.RenderBeginTag(HtmlTextWriterTag.Img);
writer.RenderEndTag();

//结束最外层Div
writer.RenderEndTag();
pre1.InnerText = sw.ToString();
Response.Write(sw.ToString());

显示结果




注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2019 ITdaan.com 粤ICP备14056181号