标签:property 用户名 复杂 简单 page on() doc 密码 inpu
区分Control类和WebControl类:
服务器控件可以继承自这两个类;
探究这个两个类的区别,以及这两个类分别适用在哪种情况!
本文的学习过程中,使用到的类库为ControlLibrary,这个类库可以自己命名;
首先编写一个继承自Control类的SimpleControl控件:类库中新建一个SimpleControl类
using System; using System.Web; using System.Web.UI; namespace ControlLibrary {
//继承Control类 public class SimpleControl:Control {
//重载Render方法 protected override void Render(HtmlTextWriter writer) {
//调用HtmlTextWriter的Write方法,向页面写入内容 writer.Write("This is my first Control"); } } }
再编写一个继承自WebControl类的SimpleWebControl;类库中新建一个SimpleWebControl类
具体代码:
using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace ControlLibrary { public class SimpleWebControl:WebControl { private int _number; public int Number { get { return _number; } set { _number = value; } }
//默认标签为<span> protected override HtmlTextWriterTag TagKey { get { return HtmlTextWriterTag.Div; } } protected override void RenderContents(System.Web.UI.HtmlTextWriter writer) { writer.Write("This is my first WebControl"); writer.Write("<br>"); writer.Write("this WebControl property is Number"); writer.Write("<br>"); writer.Write("this Number is :"); writer.Write(Number); } } }
页面中代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebSite.Default" %>
//服务器控件注册 <%@ Register TagPrefix="nsp" Namespace="ControlLibrary" Assembly="ControlLibrary" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <link rel="stylesheet" href="Style/Site.css" type="text/css" /> <title></title> </head> <body> <nsp:SimpleControl runat="server"/> <br/> <br/> <nsp:SimpleWebControl runat="server"/> </body> </html>
通过这两个简单的控件(单标签),我们可以发现一些基础的区别:
继承自Control的控件:
通过重载Render来向Html响应流中写入内容;
无默认标签,直接写入页面中
不带有样式设计
继承自WebControl的控件:
通过重载RenderContents来向Html写入内容;
默认一个<span>标签,并将内容存放进入(重载HtmlTextWriterTag可以使用其他标签)
默认带有样式设计
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
分别用继承自Control和WebControl,来编写一个复杂的控件(多标签):
继承自Control,新建一个ComplexControl类:
using System; using System.Web; using System.Web.UI; namespace ControlLibrary { public class ComplexControl:Control { private string _name; public string Name { get { return _name; } set { _name = value; } } //确定此控件没有子控件 protected override ControlCollection CreateControlCollection() { return new EmptyControlCollection(this); } protected override void Render(HtmlTextWriter writer) { writer.AddAttribute(HtmlTextWriterAttribute.Id,this.UniqueID); writer.AddStyleAttribute(HtmlTextWriterStyle.BorderColor,"red"); writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle,"solid"); writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "1px"); writer.RenderBeginTag(HtmlTextWriterTag.Table); writer.RenderBeginTag(HtmlTextWriterTag.Tr); writer.RenderBeginTag(HtmlTextWriterTag.Td); writer.Write("请输入用户名"); writer.RenderEndTag();//td writer.RenderBeginTag(HtmlTextWriterTag.Td); writer.AddAttribute(HtmlTextWriterAttribute.Name,"Username"); writer.RenderBeginTag(HtmlTextWriterTag.Input); writer.RenderEndTag();//input writer.RenderEndTag();//td writer.RenderEndTag();//tr writer.RenderBeginTag(HtmlTextWriterTag.Tr); writer.RenderBeginTag(HtmlTextWriterTag.Td); writer.Write("请输入密码"); writer.RenderEndTag();//td writer.RenderBeginTag(HtmlTextWriterTag.Td); writer.AddAttribute(HtmlTextWriterAttribute.Name, "Password"); writer.RenderBeginTag(HtmlTextWriterTag.Input); writer.RenderEndTag();//input writer.RenderEndTag();//td writer.RenderEndTag();//tr writer.RenderBeginTag(HtmlTextWriterTag.Tr); writer.RenderBeginTag(HtmlTextWriterTag.Td); writer.AddAttribute(HtmlTextWriterAttribute.Type,"button"); writer.AddAttribute(HtmlTextWriterAttribute.Value,"登录"); writer.RenderBeginTag(HtmlTextWriterTag.Input); writer.RenderEndTag();//input writer.RenderEndTag();//td writer.RenderEndTag();//tr writer.RenderEndTag();//table } } }
标签:property 用户名 复杂 简单 page on() doc 密码 inpu
原文地址:http://www.cnblogs.com/shenqizhu/p/7262082.html