如何创建一个pragraph< p>使用HtmlGenericControl类在ASP.NET中标记?
答案 0 :(得分:27)
HtmlGenericControl para = new HtmlGenericControl ( "p" );
虽然我会把它留作额外属性/方法的容器控件。
HtmlContainerControl para = (HtmlContainerControl)new HtmlGenericControl ( "p" );
答案 1 :(得分:4)
我知道,也许不是最快的实现方式,但我曾经写过“丢失”的html控件,以后可以通过代码重用,而不会搞乱html标签。
Public Class HtmlParagraph
Inherits HtmlControl
Public Sub New()
MyBase.New("p")
End Sub
Protected Overrides Sub AddParsedSubObject(ByVal obj As Object)
If (TypeOf obj Is LiteralControl) Then
Me._text = DirectCast(obj, LiteralControl).Text
Else
MyBase.AddParsedSubObject(obj)
End If
End Sub
Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
writer.RenderBeginTag(HtmlTextWriterTag.Fieldset)
If (Me.HasControls) Then
Me.RenderChildren(writer)
ElseIf (Not String.IsNullOrEmpty(Me._text)) Then
writer.Write(Me._text)
End If
writer.RenderEndTag()
End Sub
Private pText As String = String.Empty
<Category("Appearance"), PersistenceMode(PersistenceMode.InnerDefaultProperty), Localizable(True), DefaultValue(""), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
Public Overridable Property [Text]() As String
Get
If (Me.pText Is Nothing) Then Return String.Empty Else Return Me.pText
End Get
Set(ByVal value As String)
Me.pText = value
End Set
End Property
End Class
答案 2 :(得分:-7)
new HtmlGenericControl("p");
PS。尝试使用Intellisense ...