服務器控件開發(二)
區分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 } } }
服務器控件開發(二)