当前位置:首页 > 服务器控件开发(2)—— WebControl

服务器控件开发(2)—— WebControl

点击次数:1599  更新日期:2010-12-31
\n

上一篇里我们介绍了Control 基类, 这一篇我们来看看WebControl 类。 Asp.Net 里面的大部分控件都是从WebControl 继承的, WebControl 与 Control 相比。 提供了一系列支持控件样式的属性。 如果你的控件需要相客户端呈现HTML标签。
从WebControl 继承将省去你不少的工作。 这些属性以及说明列举如下:
AccessKey
String
The keyboard shortcut key used to set focus on the rendered HTML element.

Attributes
AttributeCollection
A collection of custom name/value pairs rendered as attributes on the HTML element rendered by the control. We’ll describe this property in Chapter 10.

BackColor
Color
The background color of the HTML element rendered by the control.

BorderColor
Color
The color of the border around the HTML element rendered by the control.

BorderStyle
BorderStyle
The border style of the HTML element rendered by the control, such as solid, double, or dotted.

BorderWidth
Unit
The thickness of the border around the HTML element rendered by the control.

ControlStyle
Style
The strongly typed style of the control that encapsulates all its appearance-related functionality. We’ll describe ControlStyle in Chapter 11, “Styles in Controls.” The type of Control-Style is a class that derives from System.Web.UI.Web-Controls.Style, which exposes properties such as Font, Width, BackColor, and ForeColor.

CssClass
String
The CSS class rendered by the control on the client. Page developers can use this property to associate the rendered element with style attributes declared in a style sheet.

Enabled
Boolean
Specifies whether the HTML element rendered by a Web server control is enabled. Typically, an HTML element that is not enabled is dimmed in the Web browser and cannot receive input focus.

Font
FontInfo
The font used to display the text within the HTML element rendered by the control.

ForeColor
Color
The foreground color of the text within the HTML element rendered by the control.

Height
Unit
The height of the HTML element rendered by the control.

ToolTip
String
The text shown in the ToolTip that is displayed when the cursor is situated over the HTML element rendered by the control.

Width
Unit
The width of the HTML element rendered by the control.

WebControl 还包含2 个受保护的属性。

TagKey
HtmlTextWriterTag
Override this property to render a standard HTML tag that is included in the System.Web.UI.HtmlTextWriterTag enumeration instead of the default < span > tag that WebControl renders.

TagName
String
Override this property to render a nonstandard HTML tag (one that is not included in the System.Web.UI.HtmlTextWriterTag enumeration) instead of the default < span > tag.

从上一章中我们知道重写控件的最直接的方法就是重写控件的Render 方法。让我们来看看WebControl 是如何重写Control类的Render 方法的。
#region
protected override void Render(HtmlTextWriter writer)
{
RenderBeginTag(writer);
RenderContents(writer);
RenderEndTag(writer);
}

public virtual void RenderBeginTag(HtmlTextWriter writer)
{
AddAttributesToRender(writer);
writer.RenderBeginTag(TagName);
}

public virtual void RenderEndTag(HtmlTextWriter writer)
{
writer.RenderEndTag();
}

protected virtual string TagName
{
get
{
if (tagName == null && TagKey != 0)
{
tagName = Enum.Format(typeof(HtmlTextWriterTag), TagKey, “G”).ToString();
}
else
{
tagName = “Span”;
}
// What if tagName is null and tagKey 0?
return tagName;
}
}

protected virtual void AddAttributesToRender(HtmlTextWriter writer)
{
if (ID != null)
{
writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
}
if (AccessKey.Length > 0)
{
writer.AddAttribute(HtmlTextWriterAttribute.Accesskey, AccessKey);
}
if (!Enabled)
{
writer.AddAttribute(HtmlTextWriterAttribute.Disabled, “disabled”);
}
if (ToolTip.Length > 0)
{
writer.AddAttribute(HtmlTextWriterAttribute.Title, ToolTip);
}
if (TabIndex != 0)
{
writer.AddAttribute(HtmlTextWriterAttribute.Tabindex, TabIndex.ToString());
}
if (ControlStyleCreated)
{
if (!ControlStyle.IsEmpty)
{
ControlStyle.AddAttributesToRender(writer, this);
}
}
if (attributeState != null)
{
IEnumerator ie = Attributes.Keys.GetEnumerator();
while (ie.MoveNext())
{
string key = (string)ie.Current;
writer.AddAttribute(key, Attributes[key]);
}
}


protected virtual void RenderContents(HtmlTextWriter writer)
{
base.Render(writer);
}
#endregion从上面的代码我们可以看出WebControl 类 Render 出一个 以“TagName”属性所决定的HTML 包装器。该包装器是型如: <span class = “” ID = “” title =”">Content</Span>的HTML
其主要作用就是为控件的样式提供一个载体。我们可以通过重写TagName, 或者 TagKey 属性指定任何我们需要的HTML元素作为控件的包装器。
同时WebControl 为我们提供了 一个叫做 RenderContents(HtmlTextWriter writer) 的虚方法。该方法只是简单的调用其基类的Render 方法。

基于以上认识。 我们可以从以下3 种方式来实现自定义逻辑。

1。 重写 RenderContents 方法 自定义包装器 内部 HTML 呈现内容

2。 重写 AddAttributeToRender 方法 来实现 除了 WebControl 默认属性之外的 自定义 属性。

3。 重写 TagName / TagKey 属性 来自定一作为包装器的HTML 元素。

4。 重写 Render 方法(可以重用AddAttributeToRender 的实现)来自定义整个控件的呈现。

让我们以Button 控件为例。 来看看Button 控件是如何Render HTML 的。 首先拖一个Button 控件到页面上,然后查看HTML源。 可以看到Button 控件最终呈现的HTMl元素
为: <input type=”submit” name=”Button6″ value=”FormAuth” id=”Button6″ style=”width:146px;” />。 让我们再来看看Button 控件的AddAttributesToRender 方法的实现
#region
protected override HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTag.Input; //使用Input 标签作为包装器。
}
}

protected override void AddAttributesToRender (HtmlTextWriter writer)
{
if (Page != null)
Page.VerifyRenderingInServerForm (this); // 该方法检查控件是否包含在具有Ruant = “Server” 标记的Form 元素之内。


\n

writer.AddAttribute (HtmlTextWriterAttribute.Type, “submit”); // 添加submit 属性
writer.AddAttribute (HtmlTextWriterAttribute.Name, base.UniqueID); // 添加name 属性
writer.AddAttribute (HtmlTextWriterAttribute.Value, Text); // 添加value 属性
if (Page != null && CausesValidation && Page.Validators.Count > 0) {
writer.AddAttribute (System.Web.UI.HtmlTextWriterAttribute.onclick,
Utils.GetClientValidatedEvent (Page));
writer.AddAttribute (“language”, “javascript”);
}
base.AddAttributesToRender (writer);
}

protected override void RenderContents(HtmlTextWriter writer)
{
// Preventing base classes to do anything
}
#endregion 再来看看 ListBox 控件的实现:
#region
protected override HtmlTextWriterTag TagKey {
get { return HtmlTextWriterTag.Select; }
}

protected override void AddAttributesToRender (HtmlTextWriter writer)
{
if (Page != null)
Page.VerifyRenderingInServerForm (this);


\n

writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
base.AddAttributesToRender (writer);
writer.AddAttribute (HtmlTextWriterAttribute.Size,
Rows.ToString (NumberFormatInfo.InvariantInfo));


\n

if (SelectionMode == ListSelectionMode.Multiple)
writer.AddAttribute (HtmlTextWriterAttribute.Multiple, “multiple”);


\n

if (AutoPostBack && Page != null){
writer.AddAttribute (HtmlTextWriterAttribute.Onchange,
Page.GetPostBackClientEvent (this, “”));
writer.AddAttribute (“language”, “javascript”);
}
}

protected override void RenderContents (HtmlTextWriter writer)
{
bool isMultAllowed = (SelectionMode == ListSelectionMode.Multiple);
bool selMade = false;
foreach (ListItem current in Items){
writer.WriteBeginTag (“option”);
if (current.Selected){
if (!isMultAllowed && selMade)
throw new HttpException (“Cannot_MultiSelect_In_Single_Mode”);
selMade = true;
writer.WriteAttribute (“selected”, “selected”);
}
writer.WriteAttribute (“value”, current.Value, true);
writer.Write (‘>’);
writer.Write (HttpUtility.HtmlEncode (current.Text));
writer.WriteEndTag (“option”);
writer.WriteLine ();
}
}
#endregion
一个简单ListBox 最终呈现的HTML 代码为 :
<select size=”4″ name=”ListBox1″ id=”ListBox1″ style=”height:211px;width:249px;”>
<option value=”"></option>
<option value=”"></option>
<option value=”"></option>
</select>
到此相信大家对于WebControl 是如何从Control 类继承。 以及我们该从WebControl 类继承以最小化我们的工作应该有个初步的了解了吧。
下一篇文章我们一起来学习 组合控件 是如何重Control 继承, 以及如何开发组合控件。


\n

来源:http://www.cnblogs.com/wwwyfjp

\n