<Browsable(True), _ Category("行为"), _ Description("成功提交新的 Blog 条目后, " & _ "应重定向到的 " & _ "页面的 URL。"), _ Editor("System.Web.UI.Design.UrlEditor", _ GetType(UITypeEditor))> _ Public Property AddRedirect() As String @#属性进程代码 End Property |
<AttributeName(AttributeParams)> |
<Assembly: TagPrefix("BlogControl", "BlogControl")> |
<ToolboxData("<{0}:Blog_DT runat=server></{0}:Blog_DT>")> _ Public Class Blog_DT Inherits Panel Implements INamingContainer @#控件实现 End Class |
Public Class BlogDesigner Inherits ControlDesigner Public Overrides Function GetDesignTimeHtml() As String Return "<h1>Blog</h1><hr/><hr/>" End Function End Class |
<Description("简单 Blog 控件。支持显示 " & _ "Web 日志/来自 XML 文件的新条目。"), _ Designer("BlogControl.BlogDesigner"), _ ToolboxData("<{0}:Blog_DT runat=server></{0}:Blog_DT>")> _ Public Class Blog_DT Inherits Panel Implements INamingContainer |
注释 | 用途 | 有效值 |
vs:absolutepositioning | 在根 <schema> 元素上使用,用于确定 Visual Studio 是否可以插入用于定位的样式特性。 | true 或 false |
vs:blockformatted | 表明是否可以在自动格式化期间为元素添加前导空格。 | true 或 false |
vs:builder | 指定用于编辑相关属性值的生成器。 | 颜色、样式或 URL |
vs:deprecated | 允许将某个相关属性标记为“已否决”,以防止其在属性浏览器和语句完成中出现。 | true 或 false |
vs:empty | 在元素级别使用,用于指示 Visual Studio .NET 应对相关标记(无结束标记)使用一个标记语法。 | true 或 false |
vs:friendlyname | 在根级别使用,用于为架构提供显示名。 | |
vs:iscasesensitive | 在根级别使用,说明 Visual Studio .NET 是否以区分大小写的方式处理相关标记。 | true 或 false |
vs:ishtmlschema | 在根级别使用,说明架构是否是一个 HTML 文档架构。 | true 或 false |
vs:nonbrowseable | 在特性级别使用,说明该特性不应出现在语句完成中。 | true 或 false |
vs:readonly | 在特性级别使用,说明不能在属性窗口中修改该特性。 | true 或 false |
vs:requireattributequotes | 在根级别使用,说明特性值必须用引号括起。 | true 或 false |
<body xmlns:BlogControl="urn:http://www.aspnetian.com/schemas"> |
@#supports Color structure Imports System.Drawing @#支持 StreamWriter 类型 Imports System.IO Imports System.Web.UI @#支持使用 HTML 控件 Imports System.Web.UI.HtmlControls @#支持使用 Web 控件 Imports System.Web.UI.WebControls Public Class Blog Inherits Panel Implements INamingContainer Protected BlogDS As DataSet Protected TitleTB As TextBox Protected BlogText As TextBox Private _addRedirect As String Private _email As String Private _mode As String Private _separatorColor As Color = Color.Black Public Property AddRedirect() As String Get Return Me._addRedirect End Get Set(ByVal Value As String) Me._addRedirect = Value End Set End Property Public Property Email() As String Get Return Me._email End Get Set(ByVal Value As String) Me._email = Value End Set End Property Public Property Mode() As String Get Return Me._mode End Get Set(ByVal Value As String) Me._mode = Value End Set End Property Public Property SeparatorColor() As Color Get Return Me._separatorColor End Get Set(ByVal Value As Color) Me._separatorColor = Value End Set End Property Protected Overrides Sub OnInit(ByVal e As EventArgs) LoadData() MyBase.OnInit(e) End Sub Protected Overrides Sub CreateChildControls() If Not Me._mode = "Add" Then DisplayBlogs() Else NewBlog() End If End Sub Protected Sub LoadData() BlogDS = New DataSet() Try BlogDS.ReadXml(Page.Server.MapPath("Blog.xml")) Catch fnfEx As FileNotFoundException CreateBlankFile() LoadData() End Try End Sub Protected Sub DisplayBlogs() Dim BlogDate As DateTime Dim CurrentDate As DateTime = New DateTime() Dim BlogRows As DataRowCollection = _ BlogDS.Tables(0).Rows Dim BlogDR As DataRow For Each BlogDR In BlogRows Dim BDate As String = BlogDR("date").ToString() BlogDate = New DateTime _ (Convert.ToInt32(BDate.Substring(4, 4)), _ Convert.ToInt32(BDate.Substring(0, 2)), _ Convert.ToInt32(BDate.Substring(2, 2))) If Not CurrentDate = BlogDate Then Dim TempDate As Label = New Label() TempDate.Text = BlogDate.ToLongDateString() TempDate.Font.Size = FontUnit.Large TempDate.Font.Bold = True Me.Controls.Add(TempDate) Me.Controls.Add _ (New LiteralControl("<br/><br/>")) CurrentDate = BlogDate End If Dim Anchor As HtmlAnchor = New HtmlAnchor() Anchor.Name = "#" & BlogDR("anchorID").ToString() Me.Controls.Add(Anchor) Dim Title As Label = New Label() Title.Text = BlogDR("title").ToString() Title.Font.Size = FontUnit.Larger Title.Font.Bold = True Me.Controls.Add(Title) Me.Controls.Add(New LiteralControl("<p>")) Dim BlogText As LiteralControl = _ New LiteralControl("<div>" & _ BlogDR("text").ToString() & "</div>") Me.Controls.Add(BlogText) Me.Controls.Add(New LiteralControl("</p>")) Dim Email As HyperLink = New HyperLink() Email.NavigateUrl = "mailto:" & _ BlogDR("email").ToString() Email.Text = "E-mail me" Me.Controls.Add(Email) Me.Controls.Add(New LiteralControl(" | ")) Dim AnchorLink As HyperLink = New HyperLink() AnchorLink.NavigateUrl = _ Page.Request.Url.ToString() & "#" & _ BlogDR("anchorID").ToString() AnchorLink.Text = "Link" Me.Controls.Add(AnchorLink) Me.Controls.Add(New _ LiteralControl("<hr color=@#" & _ ColorTranslator.ToHtml(_separatorColor) & _ "@# width=@#100%@#/><br/>")) Next End Sub Protected Sub NewBlog() Dim Title As Label = New Label() Title.Text = "Create New Blog" Title.Font.Size = FontUnit.Larger Title.Font.Bold = True Me.Controls.Add(Title) Me.Controls.Add(New LiteralControl("<br/><br/>")) Dim TitleLabel As Label = New Label() TitleLabel.Text = "Title: " TitleLabel.Font.Bold = True Me.Controls.Add(TitleLabel) TitleTB = New TextBox() Me.Controls.Add(TitleTB) Me.Controls.Add(New LiteralControl("<br/>")) Dim BlogTextLabel As Label = New Label() BlogTextLabel.Text = "Text: " BlogTextLabel.Font.Bold = True Me.Controls.Add(BlogTextLabel) BlogText = New TextBox() BlogText.TextMode = TextBoxMode.MultiLine BlogText.Rows = 10 BlogText.Columns = 40 Me.Controls.Add(BlogText) Me.Controls.Add(New LiteralControl("<br/>")) Dim Submit As Button = New Button() Submit.Text = "Submit" AddHandler Submit.Click, AddressOf Me.Submit_Click Me.Controls.Add(Submit) End Sub Protected Sub Submit_Click(ByVal Sender As Object, _ ByVal e As EventArgs) EnsureChildControls() AddBlog() End Sub Protected Sub AddBlog() Dim NewBlogDR As DataRow NewBlogDR = BlogDS.Tables(0).NewRow() NewBlogDR("date") = FormatDate(DateTime.Today) NewBlogDR("title") = TitleTB.Text NewBlogDR("text") = BlogText.Text NewBlogDR("anchorID") = Guid.NewGuid().ToString() NewBlogDR("email") = _email BlogDS.Tables(0).Rows.InsertAt(NewBlogDR, 0) BlogDS.WriteXml(Page.Server.MapPath("Blog.xml")) Page.Response.Redirect(_addRedirect) End Sub Protected Function FormatDate(ByVal dt As DateTime) _ As String Dim retString As String retString = String.Format("{0:D2}", dt.Month) retString &= String.Format("{0:D2}", dt.Day) retString &= String.Format("{0:D2}", dt.Year) Return retString End Function Public Sub CreateBlankFile() Dim NewXml As StreamWriter = _ File.CreateText(Page.Server.MapPath("Blog.xml")) NewXml.WriteLine("<blogs>") NewXml.WriteLine _ (" <!-- blog field describes a single blog -->") NewXml.WriteLine(" <blog>") NewXml.WriteLine(" <!-- date field contains" & _ " the creation date of the blog -->") NewXml.WriteLine(" <date>" & _ FormatDate(DateTime.Today) & "</date>") NewXml.WriteLine _ (" <title>Temporary Blog</title>") NewXml.WriteLine(" <!-- text field " & _ "should contain the blog text, including any " & _ "desired HTML tags -->") NewXml.WriteLine(" <text>This entry " & _ "indicates that the file blog.xml was not " & _ "found.A default version of this file has " & _ "been created for you.You can modify the " & _ "fields in this file as desired.If you set " & _ "the Blog control to add mode (add the " & _ "attribute mode=@#add@# to the control@#s " & _ "declaration), the control will " & _ "automatically populate the XML file when " & _ "you submit the form.</text>") NewXml.WriteLine(" <!-- anchorID field " & _ "will be autopopulated by the control -->") NewXml.WriteLine(" <anchorID></anchorID>") NewXml.WriteLine(" <!-- email field should" & _ " contain the email address for feedback -->") NewXml.WriteLine(" <email>change this to a " & _ "valid email address</email>") NewXml.WriteLine(" </blog>") NewXml.WriteLine("</blogs>") NewXml.Close() End Sub End Class |
<%@ Register TagPrefix="cc1" Namespace="BlogControl" Assembly="BlogControl" %> <%@ Page Language="vb" AutoEventWireup="false" Codebehind="BlogClient.aspx.vb" Inherits="BlogControlClient.WebForm1"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Blog Client</title> </head> <body> <form id=Form1 method=post runat="server"> <p><asp:hyperlink id=Link1 navigateurl="BlogClient.aspx?mode=add" runat="server">Add Blog</asp:hyperlink></p> <cc1:blog id=Blog1 Email="andrew@graymad.com" AddRedirect="BlogClient.aspx" SeparatorColor="LawnGreen" runat="server"></cc1:blog> <p><asp:hyperlink id=Link2 navigateurl="BlogClient.aspx?mode=add" runat="server">Add Blog</asp:hyperlink></p> </form> </body> </html> |
Imports BlogControl Public Class WebForm1 Inherits System.Web.UI.Page Protected WithEvents Link1 As _ System.Web.UI.WebControls.HyperLink Protected WithEvents Link2 As _ System.Web.UI.WebControls.HyperLink Protected WithEvents Blog1 As BlogControl.Blog Private Sub Page_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load If Request.QueryString("mode") = "add" Then Blog1.Mode = "Add" Link1.Visible = False Link2.Visible = False Else Blog1.Mode = "Display" Link1.Visible = True Link2.Visible = True End If End Sub End Class |
@#支持设计时特性 Imports System.ComponentModel @#支持颜色结构 Imports System.Drawing @#支持 UITypeEditor 类型 Imports System.Drawing.Design @#支持 StreamWriter 类型 Imports System.IO Imports System.Web.UI @#支持 ControlDesigner 类型 @# 请注意,必须添加程序集 @# System.Design 的引用,才能导入此命名空间 Imports System.Web.UI.Design @#支持使用 HTML 控件 Imports System.Web.UI.HtmlControls @#支持使用 Web 控件 Imports System.Web.UI.WebControls <Assembly: TagPrefix("BlogControl", "BlogControl")> Public Enum BlogMode Add Display End Enum <Description("Simple Blog control.Supports display " & _ "of Web log / news items from an XML file."), _ Designer("BlogControl.BlogDesigner"), _ ToolboxData("<{0}:Blog_DT runat=server></{0}:Blog_DT>")> _ Public Class Blog_DT Inherits Panel Implements INamingContainer Protected BlogDS As DataSet Protected TitleTB As TextBox Protected BlogText As TextBox Private _addRedirect As String Private _email As String Private _mode As BlogMode Private _separatorColor As Color = Color.Black <Browsable(True), _ Category("Behavior"), _ Description("URL to which the page should redirect after successful submission of a new Blog entry."), _ Editor("System.Web.UI.Design.UrlEditor", _ GetType(UITypeEditor))> _ Public Property AddRedirect() As String Get Return Me._addRedirect End Get Set(ByVal Value As String) Me._addRedirect = Value End Set End Property <Browsable(True), _ Category("Behavior"), _ Description("Email address the control will use for listing in new Blog entries.")> _ Public Property Email() As String Get Return Me._email End Get Set(ByVal Value As String) Me._email = Value End Set End Property <Browsable(True), _ Category("Behavior"), _ Description("Controls whether existing Blogs are displayed, or fields for creating a new Blog entry.")> _ Public Property Mode() As BlogMode Get Return Me._mode End Get Set(ByVal Value As BlogMode) Me._mode = Value End Set End Property <Browsable(True), _ Category("Appearance"), _ Description("Controls the color of the line that separates Blog entries when in display mode.")> _ Public Property SeparatorColor() As Color Get Return Me._separatorColor End Get Set(ByVal Value As Color) Me._separatorColor = Value End Set End Property Protected Overrides Sub OnInit(ByVal e As EventArgs) LoadData() MyBase.OnInit(e) End Sub Protected Overrides Sub CreateChildControls() If Not Me._mode = BlogMode.Add Then DisplayBlogs() Else NewBlog() End If End Sub Protected Sub LoadData() BlogDS = New DataSet() Try BlogDS.ReadXml(Page.Server.MapPath("Blog.xml")) Catch fnfEx As FileNotFoundException CreateBlankFile() LoadData() End Try End Sub Protected Sub DisplayBlogs() Dim BlogDate As DateTime Dim CurrentDate As DateTime = New DateTime() Dim BlogRows As DataRowCollection = _ BlogDS.Tables(0).Rows Dim BlogDR As DataRow For Each BlogDR In BlogRows Dim BDate As String = BlogDR("date").ToString() BlogDate = New DateTime _ (Convert.ToInt32(BDate.Substring(4, 4)), _ Convert.ToInt32(BDate.Substring(0, 2)), _ Convert.ToInt32(BDate.Substring(2, 2))) If Not CurrentDate = BlogDate Then Dim TempDate As Label = New Label() TempDate.Text = BlogDate.ToLongDateString() TempDate.Font.Size = FontUnit.Large TempDate.Font.Bold = True Me.Controls.Add(TempDate) Me.Controls.Add _ (New LiteralControl("<br/><br/>")) CurrentDate = BlogDate End If Dim Anchor As HtmlAnchor = New HtmlAnchor() Anchor.Name = "#" + BlogDR("anchorID").ToString() Me.Controls.Add(Anchor) Dim Title As Label = New Label() Title.Text = BlogDR("title").ToString() Title.Font.Size = FontUnit.Larger Title.Font.Bold = True Me.Controls.Add(Title) Me.Controls.Add(New LiteralControl("<p>")) Dim BlogText As LiteralControl = _ New LiteralControl("<div>" & _ BlogDR("text").ToString() & "</div>") Me.Controls.Add(BlogText) Me.Controls.Add(New LiteralControl("</p>")) Dim Email As HyperLink = New HyperLink() Email.NavigateUrl = "mailto:" & _ BlogDR("email").ToString() Email.Text = "E-mail me" Me.Controls.Add(Email) Me.Controls.Add(New LiteralControl(" | ")) Dim AnchorLink As HyperLink = New HyperLink() AnchorLink.NavigateUrl = _ Page.Request.Url.ToString() & "#" & _ BlogDR("anchorID").ToString() AnchorLink.Text = "Link" Me.Controls.Add(AnchorLink) Me.Controls.Add _ (New LiteralControl("<hr color=@#" & _ ColorTranslator.ToHtml(_separatorColor) & _ "@# width=@#100%@#/><br/>")) Next End Sub Protected Sub NewBlog() Dim Title As Label = New Label() Title.Text = "Create New Blog" Title.Font.Size = FontUnit.Larger Title.Font.Bold = True Me.Controls.Add(Title) Me.Controls.Add(New LiteralControl("<br/><br/>")) Dim TitleLabel As Label = New Label() TitleLabel.Text = "Title: " TitleLabel.Font.Bold = True Me.Controls.Add(TitleLabel) TitleTB = New TextBox() Me.Controls.Add(TitleTB) Me.Controls.Add(New LiteralControl("<br/>")) Dim BlogTextLabel As Label = New Label() BlogTextLabel.Text = "Text: " BlogTextLabel.Font.Bold = True Me.Controls.Add(BlogTextLabel) BlogText = New TextBox() BlogText.TextMode = TextBoxMode.MultiLine BlogText.Rows = 10 BlogText.Columns = 40 Me.Controls.Add(BlogText) Me.Controls.Add(New LiteralControl("<br/>")) Dim Submit As Button = New Button() Submit.Text = "Submit" AddHandler Submit.Click, AddressOf Me.Submit_Click Me.Controls.Add(Submit) End Sub Protected Sub Submit_Click(ByVal Sender As Object, _ ByVal e As EventArgs) EnsureChildControls() AddBlog() End Sub Protected Sub AddBlog() Dim NewBlogDR As DataRow NewBlogDR = BlogDS.Tables(0).NewRow() NewBlogDR("date") = FormatDate(DateTime.Today) NewBlogDR("title") = TitleTB.Text NewBlogDR("text") = BlogText.Text NewBlogDR("anchorID") = Guid.NewGuid().ToString() NewBlogDR("email") = _email BlogDS.Tables(0).Rows.InsertAt(NewBlogDR, 0) BlogDS.WriteXml(Page.Server.MapPath("Blog.xml")) Page.Response.Redirect(_addRedirect) End Sub Protected Function FormatDate(ByVal dt As DateTime) As String Dim retString As String retString = String.Format("{0:D2}", dt.Month) retString &= String.Format("{0:D2}", dt.Day) retString &= String.Format("{0:D2}", dt.Year) Return retString End Function Public Sub CreateBlankFile() Dim NewXml As StreamWriter = _ File.CreateText(Page.Server.MapPath("Blog.xml")) NewXml.WriteLine("<blogs>") NewXml.WriteLine _ (" <!-- blog field describes a single blog -->") NewXml.WriteLine(" <blog>") NewXml.WriteLine(" <!-- date field contains" & _ " the creation date of the blog -->") NewXml.WriteLine(" <date>" & _ FormatDate(DateTime.Today) & "</date>") NewXml.WriteLine _ (" <title>Temporary Blog</title>") NewXml.WriteLine(" <!-- text field " & _ "should contain the blog text, including any " & _ "desired HTML tags -->") NewXml.WriteLine(" <text>This entry " & _ "indicates that the file blog.xml was not " & _ "found.A default version of this file has " & _ "been created for you.You can modify the " & _ "fields in this file as desired.If you set " & _ "the Blog control to add mode (add the " & _ "attribute mode=@#add@# to the control@#s " & _ "declaration), the control will " & _ "automatically populate the XML file when " & _ "you submit the form.</text>") NewXml.WriteLine(" <!-- anchorID field " & _ "will be autopopulated by the control -->") NewXml.WriteLine(" <anchorID></anchorID>") NewXml.WriteLine(" <!-- email field should" & _ " contain the email address for feedback -->") NewXml.WriteLine(" <email>change this to a " & _ "valid email address</email>") NewXml.WriteLine(" </blog>") NewXml.WriteLine("</blogs>") NewXml.Close() End Sub End Class Public Class BlogDesigner Inherits ControlDesigner Public Overrides Function GetDesignTimeHtml() As String Return "<h1>Blog</h1><hr/><hr/>" End Function End Class |
<?xml version="1.0" encoding="utf-8" ?> <xsd:schema targetNamespace="urn:http://www.aspnetian.com/schemas" elementFormDefault="qualified" xmlns="urn:http://www.aspnetian.com/schemas" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:vs="http://schemas.microsoft.com/Visual-Studio-Intellisense" vs:friendlyname="Blog Control Schema" vs:ishtmlschema="false" vs:iscasesensitive="false" vs:requireattributequotes="true" > <xsd:annotation> <xsd:documentation> Blog Control schema. </xsd:documentation> </xsd:annotation> <xsd:element name="Blog_DT" type="BlogDef" /> <!-- <aspnetian:Blog> --> <xsd:complexType name="BlogDef"> <!-- <aspnetian:Blog>-specific attributes --> <xsd:attribute name="AddRedirect" type="xsd:string" vs:builder="url"/> <xsd:attribute name="Email" type="xsd:string"/> <xsd:attribute name="Mode" type="BlogMode"/> <xsd:attribute name="SeparatorColor" type="xsd:string" vs:builder="color"/> <!-- <asp:Panel>-specific attributes --> <xsd:attribute name="BackImageUrl" type="xsd:anyURI" /> <xsd:attribute name="HorizontalAlign" type="HorizontalAlign" /> <xsd:attribute name="Wrap" type="xsd:boolean" /> <xsd:attribute name="Enabled" type="xsd:boolean" /> <xsd:attribute name="BorderWidth" type="ui4" /> <xsd:attribute name="BorderColor" type="xsd:string" vs:builder="color" /> <xsd:attribute name="BorderStyle" type="BorderStyle" /> <xsd:attributeGroup ref="WebControlAttributes" /> </xsd:complexType> <!-- DataTypes --> <xsd:simpleType name="BlogMode"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="Add" /> <xsd:enumeration value="Display" /> </xsd:restriction> </xsd:simpleType> </xsd:schema> |