asp.net服务器控件创建一(HtmlTextWriter)

来源:互联网 发布:淘宝商品详情模板 编辑:程序博客网 时间:2024/05/16 06:20

HtmlTextWriter在控件制作中发挥重要作用,能够帮助我快速生成html语,HtmlTextWriter的名称空间是System.Web.UI。
HtmlTextWriter类提供以下服务
1:向输出流写入HTML或其他标记代码
2:管理结构良好的元素的创建
3:创建属性
4:创建样式属性

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();

指定可传递到 HtmlTextWriter Html32TextWriter 对象输出流的 HTML 标记。

       该枚举使输出流可以在响应 Web 请求时连同 HTML 服务器控件一起编写 HTML 标记。

       成员如下

成员名称

说明

A

指定 HTML a 元素。

Acronym

指定 HTML acronym 元素。

Address

指定 HTML address 元素。

Area

指定 HTML area 元素。

B

指定 HTML b 元素。

Base

指定 HTML base 元素。

Basefont

指定 HTML basefont 元素。

Bdo

指定 HTML bdo 元素。

Bgsound

指定 HTML bgsound 元素。

Big

指定 HTML big 元素。

Blockquote

指定 HTML blockquote 元素。

Body

指定 HTML body 元素。

Br

指定 HTML br 元素。

Button

指定 HTML button 元素。

Caption

指定 HTML caption 元素。

Center

指定 HTML center 元素。

Cite

指定 HTML cite 元素。

Code

指定 HTML code 元素。

Col

指定 HTML col 元素。

Colgroup

指定 HTML colgroup 元素。

Dd

指定 HTML dd 元素。

Del

指定 HTML cel 元素。

Dfn

指定 HTML dfn 元素。

Dir

指定 HTML dir 元素。

Div

指定 HTML div 元素。

Dl

指定 HTML dl 元素。

Dt

指定 HTML dt 元素。

Em

指定 HTML em 元素。

Embed

指定 HTML embed 元素。

Fieldset

指定 HTML fieldset 元素。

Font

指定 HTML font 元素。

Form

指定 HTML form 元素。

Frame

指定 HTML frame 元素。

Frameset

指定 HTML frameset 元素。

H1

指定 HTML H1 元素。

H2

指定 HTML H2 元素。

H3

指定 HTML H3 元素。

H4

指定 HTML H4 元素。

H5

指定 HTML H5 元素。

H6

指定 HTML H6 元素。

Head

指定 HTML head 元素。

Hr

指定 HTML hr 元素。

Html

指定 HTML html 元素。

I

指定 HTML i 元素。

Iframe

指定 HTML iframe 元素。

Img

指定 HTML img 元素。

Input

指定 HTML input 元素。

Ins

指定 HTML ins 元素。

Isindex

指定 HTML isindex 元素。

Kbd

指定 HTML kbd 元素。

Label

指定 HTML label 元素。

Legend

指定 HTML legend 元素。

Li

指定 HTML li 元素。

Link

指定 HTML link 元素。

Map

指定 HTML map 元素。

Marquee

指定 HTML marquee 元素。

Menu

指定 HTML menu 元素。

Meta

指定 HTML meta 元素。

Nobr

指定 HTML nobr 元素。

Noframes

指定 HTML noframes 元素。

Noscript

指定 HTML noscript 元素。

Object

指定 HTML object 元素。

Ol

指定 HTML ol 元素。

Option

指定 HTML option 元素。

P

指定 HTML p 元素。

Param

指定 HTML param 元素。

Pre

指定 HTML pre 元素。

Q

指定 HTML q 元素。

Rt

指定 DHTML rt 元素,后者指定 ruby 元素的文本。

Ruby

指定 DHTML ruby 元素。

S

指定 HTML s 元素。

Samp

指定 HTML samp 元素。

Script

指定 HTML script 元素。

Select

指定 HTML select 元素。

Small

指定 HTML small 元素。

Span

指定 HTML span 元素。

Strike

指定 HTML strike 元素。

Strong

指定 HTML strong 元素。

Style

指定 HTML style 元素。

Sub

指定 HTML sub 元素。

Sup

指定 HTML sup 元素。

Table

指定 HTML table 元素。

Tbody

指定 HTML tbody 元素。

Td

指定 HTML td 元素。

Textarea

指定 HTML textarea 元素。

Tfoot

指定 HTML tfoot 元素。

Th

指定 HTML th 元素。

Thead

指定 HTML thead 元素。

Title

指定 HTML title 元素。

Tr

指定 HTML tr 元素。

Tt

指定 HTML tt 元素。

U

指定 HTML u 元素。

Ul

指定 HTML ul 元素。

Unknown

不识别作为 HTML 标记传递的 String

Var

指定 HTML var 元素。

Wbr

指定 HTML wbr 元素。

Xml

指定 HTML xml 元素。

代码示例:

----------――――――――――――――――――――――――――――――――

[C#]

// Control HTML encoding of attributes.

// Simple known values do not need encoding.

writer.AddAttribute(HtmlTextWriterAttribute.Alt, "Encoding, /"Required/"", true);

writer.AddAttribute("myattribute", "No &quot;encodig &quot; required", false);

writer.RenderBeginTag(HtmlTextWriterTag.Img);

writer.RenderEndTag();

writer.WriteLine();

----------――――――――――――――――――――――――――――――――

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之前。

指定 HTML 属性,在处理 Web 请求时,HtmlTextWriter Html32TextWriter 对象将该属性写入 HTML 元素的开始标记。

此枚举经常与 HtmlTextWriter 类的下列方法联合使用:AddAttributeGetAttributeNameIsAttributeDefined RenderBeginTag

成员如下:

 

成员名称

说明

Accesskey

指定将 HTML acesskey 属性写入标记。

Align

指定将 HTML align 属性写入标记。

Alt

指定将 HTML alt 属性写入标记。

Background

指定将 HTML background 属性写入标记。

Bgcolor

指定将 HTML bgcolor 属性写入标记。

Border

指定将 HTML border 属性写入标记。

Bordercolor

指定将 HTML bordercolor 属性写入标记。

Cellpadding

指定将 HTML cellpadding 属性写入标记。

Cellspacing

指定将 HTML cellspacing 属性写入标记。

Checked

指定将 HTML checked 属性写入标记。

Class

指定将 HTML class 属性写入标记。

Cols

指定将 HTML cols 属性写入标记。

Colspan

指定将 HTML colspan 属性写入标记。

Disabled

指定将 HTML disabled 属性写入标记。

For

指定将 HTML for 属性写入标记。

Height

指定将 HTML height 属性写入标记。

Href

指定将 HTML href 属性写入标记。

Id

指定将 HTML id 属性写入标记。

Maxlength

指定将 HTML maxlength 属性写入标记。

Multiple

指定将 HTML multiple 属性写入标记。

Name

指定将 HTML name 属性写入标记。

Nowrap

指定将 HTML nowrap 属性写入标记。

Onchange

指定将 HTML onchange 属性写入标记。

Onclick

指定将 HTML onclick 属性写入标记。

ReadOnly

指定将 HTML readonly 属性写入标记。

Rows

指定将 HTML rows 属性写入标记。

Rowspan

指定将 HTML rowspan 属性写入标记。

Rules

指定将 HTML rules 属性写入标记。

Selected

指定将 HTML selected 属性写入标记。

Size

指定将 HTML size 属性写入标记。

Src

指定将 HTML src 属性写入标记。

Style

指定将 HTML style 属性写入标记。

Tabindex

指定将 HTML tabindex 属性写入标记。

Target

指定将 HTML target 属性写入标记。

Title

指定将 HTML title 属性写入标记。

Type

指定将 HTML type 属性写入标记。

Valign

指定将 HTML valign 属性写入标记。

Value

指定将 HTML value 属性写入标记。

Width

指定将 HTML width 属性写入标记。

Wrap

指定将 HTML wrap 属性写入标记。

 

代码示例:

 

[C#]

writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "alert('Hello');");

4.HtmlTextWriterStyle指定可用于 HtmlTextWriter 或 Html32TextWriter 对象输出流的 HTML 样式。 

3HtmlTextWriterStyle

指定可用于 HtmlTextWriter Html32TextWriter 对象输出流的 HTML 样式。

HtmlTextWriter Html32TextWriter 对象使用这些枚举值将 HTML 字符串注册为正确的 HTML 样式。

成员如下:

成员名称

说明

BackgroundColor

指定 HTML backgroundcolor 样式。

BackgroundImage

指定 HTML backgroundimage 样式。

BorderCollapse

指定 HTML bordercollapse 样式。

BorderColor

指定 HTML bordercolor 样式。

BorderStyle

指定 HTML borderstyle 样式。

BorderWidth

指定 HTML borderwidth 样式。

Color

指定 HTML color 样式。

FontFamily

指定 HTML fontfamily 样式。

FontSize

指定 HTML fontsize 样式。

FontStyle

指定 HTML fontstyle 样式。

FontWeight

指定 HTML fontweight 样式。

Height

指定 HTML height 样式。

TextDecoration

指定 HTML textdecoration 样式。

Width

指定 HTML width 样式。

代码示例:

-----――――――――――――――――――――――――――――――――

[C#]

writer.AddStyleAttribute(HtmlTextWriterStyle.Color, "Red");


---------------------------------------------------------------------
*.cs
using System;
using System.Web;
using System.Web.UI;

namespace WroxControls
{
   public class MyFirstControl : Control
   {
      protected override void Render(HtmlTextWriter writer)
      {
         writer.Write("<h1>ASP.NET Control Development in C#</h1>");
      }
   }
};


*.aspx
<%@ Register TagPrefix="Wrox" Namespace="WroxControls"  Assembly="MyFirstControl" %>
<html>
<body>
   <Wrox:MyFirstControl runat="server" />
</body>
</html>

// HtmlTextWriter继承于System.IO.TextWriter,
// Render方法输出的是HtmlTextWriter类型的变量。

--------------------------控件属性--------------------------------------------
*.cs

using System;
using System.Web;
using System.Drawing;
using System.Web.UI;

namespace WroxControls
{
   public class MyLabel : Control
   {

      private string _text;

      public string Text
      {
         get{ return _text; }
         set{ _text = value; } 
      }

      protected override void OnInit(EventArgs e)
      {
         base.OnInit(e);
 
         if ( _text == null )
             _text = "Here is some default text";
      }

      protected override void Render(HtmlTextWriter writer)
      {
         write.Write("<h1>"+_text+"</h1>);
      }

   }
};

*.aspx

<%@ Register TagPrefix="Wrox" Namespace="WroxControls"  Assembly="MyLabel" %>
<html>
<body>

<Wrox:MyLabel runat="server" Text="Hello" />
<Wrox:MyLabel runat="server" Text="World" />
<Wrox:MyLabel runat="server" />

</body>
</html>

----------------------控件属性转换------------------------------------------------
*.cs

using System;
using System.Web;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls ;

namespace WroxControls
{
   public class MyLabelProps : Control
   {

      string _text;

      public string Text
      {
         get{ return _text; }
         set{ _text = value; } 
      }

      int _repeatCount = 1;
 
      public int RepeatCount
      {
         get { return _repeatCount; }
         set { _repeatCount = value; }
      }

      Color _foreColor = Color.Blue;

      public Color ForeColor
      {
         get { return _foreColor; }
         set { _foreColor = value; }
      }
      protected override void OnInit(EventArgs e)
      {
         if ( _text == null )
             _text = "Here is some default text";
      }

      protected override void Render(HtmlTextWriter writer)
      {
         int loop;
         for( loop=0; loop < _repeatCount; loop++ )
         {

            writer.AddStyleAttribute("color", ColorTranslator.ToHtml(_foreColor) );
            writer.RenderBeginTag("h1");
            writer.Write( _text );
            writer.RenderEndTag();
         }
      }

      // 使用以下方法也可以
      protected override void Render(HtmlTextWriter writer)
      {
         int loop;
         for( loop=0; loop < _repeatCount; loop++ )
         {
           write.Write("<h1 style='color:"+ColorTranslator.ToHtml(_foreColor)+"'>"+_text+"</h1>);
         }
      }
   }
};

说明:
1:Color枚举 是在System.Drawing
2:为了转换Color结构类型,将使用System.Drawing.ColorTranslator类,在代码中此类的ToHtml()方法用来将_foreColor枚举值转化为html颜色值。
3:AddStyleAttribute方法的调用告诉HtmlTextWriter,它启动了的下一个元素应该有一个指定了值的样式元素color。

*.aspx
<%@ Register TagPrefix="Wrox" Namespace="WroxControls"  Assembly="MyLabelProps" %>
<html>
<body>

<Wrox:MyLabelProps runat="server" RepeatCount="3" Text="Hello2" />
<Wrox:MyLabelProps runat="server" Text="World" ForeColor="Green" />
<Wrox:MyLabelProps runat="server" ForeColor="Orange" />

</body>
</html>

原创粉丝点击