zoukankan
html css js c++ java
HtmlTextWriter学习笔记
这两天正好在研究asp.net自定义控件制作,HtmlTextWriter在控件制作中发挥重要作用,能够帮助我快速生成html语句。因此决定写下笔记,方便以后查阅。HtmlTextWriter的名称空间是System.Web.UI,如果在winform程序下使用,不要忘了添加System.Web.dll引用。
HtmlTextWriter与Html32TextWriter
这两个类不存在继承和被继承关系。HtmlTextWriter支持Html4.0标准,而Html32TextWriter支持Html3.2标准,在msdn中一般不建议将Html32TextWriter实例话,它仅为支持低版本的浏览器而设计。
HtmlTextWriter初始化
HtmlTextWriter的构造函数要求必须传递一个与TextWriter接口兼容的实例。大家可以根据需要选择合适的TextWriter接口实现类,因为该类的选择将决定最终生成的Html文本的输出方式,例如我想把结果输出到一个TextBox中,于是我就使用了StringWriter类的实例作为参数传入:
StringWriter sw
=
new
System.IO.StringWriter();
HtmlTextWriter writer
=
new
HtmlTextWriter(sw);
如果要获得结果,只需要使用sw.toString()就可以了。注意,HtmlTextWriter本身不提供输出方法或属性。
HtmlTextWriter常用方法
1
.
void
RenderBeginTag(HtmlTextWriterTag tagKey)
用于创建Html开始标签,如
<
body
>
,这里的HtmlTextWriterTag是一个枚举类型,里面有所有的标准Html4.0标签枚举值,如果我要使用添加body标签,就是RenderBeginTag(HtmlTextWriterTag.Body)
2
.
void
RenderEndTag()
与RenderBeginTag一一对应,有几个RenderBeginTag就必须有几个RenderEndTag,当然,RenderEndTag不一定要马上跟在对应的RenderBeginTag之后,要根据Html语句决定。如下面的例子:
<
html
>
<
head
></
head
>
<
body
></
body
>
</
html
>
上面一段是html基本结构,使用HtmlTextWriter生成就是
writer.RenderBeginTag(HtmlTextWriterTag.Html);
writer.RenderBeginTag(HtmlTextWriterTag.Head);
writer.RenderEndTag();
writer.RenderBeginTag(HtmlTextWriterTag.Body);
writer.RenderEndTag();
writer.RenderEndTag();
3
.
void
AddAttribute()
用于添加标签的属性,例如
<
img
>
标签的url属性、width属性等。使用该方法时要注意一点,AddAttribute语句必须出现在对应的RenderBeginTag的前面,请看下面的例子:
writer.AddAttribute(
"
url
"
,
"
../xxx.gif
"
);
writer.AddAttribute(
"
width
"
,
"
50
"
);
writer.AddAttribute(
"
height
"
,
"
50
"
);
writer.RenderBeginTag(HtmlTextWriterTag.Img);
writer.RenderEndTag();
上面一段语句将添加
<
img url
=
"
../xxx.gif
"
width
=
"
50
"
height
=
"
50
"
/>
,可以发现所有的img标签属性的添加都在RenderBeginTag之前。
4
.
void
Write()和void WriteLine()
用于写除标准Html标签以外的所有信息。
喜欢请赞赏一下啦^_^
查看全文
相关阅读:
python常用模块: random模块, time模块, sys模块, os模块, 序列化模块
(python)getattr等用法
(Pytorch)涉及的常见操作
对PatchGAN的感知域(receptive_field)理解
心得
dilated conv、deconv、fractional-strided conv
(PatchGANs)Pecomputed Real-time Texture Synthesis With Markovian Generative Adversarial Networks
(Pixel2PixelGANs)Image-to-Image translation with conditional adversarial networks
CGANs
Tensorflow--Debug
原文地址:https://www.cnblogs.com/amadeuslee/p/3744584.html
最新文章
STL: remove
STL: vector range constructor, set::insert
Linux常用的几种基本命令 使用方法
Pyhton基础语法的一些使用方法
python冒泡算法详解
使用Python防止SQL注入有那些方法?
Python 实现找出所有水仙花数的方法
python字符串的操作
python 字符串的表示方法
Python 动态变量名定义与调用方法
热门文章
Python中的浅拷贝以及深拷贝
BOM和DOM
JavaScript
线程
CSS
HTML
进程
python 网络编程
python 异常处理
python模块: hashlib模块, configparse模块, logging模块,collections模块
Copyright © 2011-2022 走看看