ButtonDesigner.GetDesignTimeHtml 方法
定义
获取用于在设计时呈现关联控件的标记。Gets the markup that is used to render the associated control at design time.
public:
override System::String ^ GetDesignTimeHtml();
public override string GetDesignTimeHtml ();
override this.GetDesignTimeHtml : unit -> string
Public Overrides Function GetDesignTimeHtml () As String
返回
一个 String,其中包含用于在设计时呈现 Button 的标记。A String containing the markup used to render the Button at design time.
示例
下面的代码示例演示如何重写 GetDesignTimeHtml 方法以更改生成的标记。The following code example demonstrates how to override the GetDesignTimeHtml method to change the generated markup.
如果 BorderStyle 以前未设置属性 (也就是说,它具有 NotSet 字段值) ,则对方法的调用 GetDesignTimeHtml 会将其设置为宽度为三个像素的蓝色虚线边框,然后在设计图面上显示该边框。If the BorderStyle property has not been set previously (that is, it has the NotSet field value), a call to the GetDesignTimeHtml method sets it to a blue-dashed border with a width of three pixels, and then displays that border on the design surface. 如果 BorderStyle 已设置该属性,则将显示现有的边框属性值。If the BorderStyle property has been set, the existing border property values are displayed.
通常, GetDesignTimeHtml 调用其基方法, ControlDesigner.GetDesignTimeHtml 该方法调用 Control.RenderControl 关联控件的方法来生成标记。Typically, the GetDesignTimeHtml calls its base method, ControlDesigner.GetDesignTimeHtml, which calls into the Control.RenderControl method of the associated control to generate the markup.
' Create a class that derives from ButtonDesigner
' and displays the custom SampleButton control
' on the design surface.
Imports System.Web.UI.Design
Imports System.Drawing
Imports System.ComponentModel
Imports System.Web.UI.WebControls
Imports System.Web.UI.Design.WebControls
Namespace Examples.AspNet
Public Class SampleButtonDesigner
Inherits ButtonDesigner
' Override the GetDesignTimeHtml method.
Public Overrides Function GetDesignTimeHtml() As String
Dim sampleButton As SampleButton = CType(Component, SampleButton)
Dim designTimeHtml As String = Nothing
' Check the control's BorderStyle property
' to conditionally render design-time HTML.
If (sampleButton.BorderStyle = BorderStyle.NotSet) Then
' Create variables to hold current property settings.
Dim oldBorderStyle As BorderStyle = sampleButton.BorderStyle
Dim oldBorderWidth As Unit = sampleButton.BorderWidth
Dim oldBorderColor As Color = sampleButton.BorderColor
' Set properties and the design-time HTML.
Try
sampleButton.BorderStyle = BorderStyle.Dashed
sampleButton.BorderWidth = Unit.Pixel(3)
sampleButton.BorderColor = Color.Blue
designTimeHtml = MyBase.GetDesignTimeHtml()
' If an exception occurs, call the GetErrorDesignTimeHtml
' method.
Catch ex As Exception
designTimeHtml = GetErrorDesignTimeHtml(ex)
' Return properties to their original settings.
Finally
sampleButton.BorderStyle = oldBorderStyle
sampleButton.BorderWidth = oldBorderWidth
sampleButton.BorderColor = oldBorderColor
End Try
Else
designTimeHtml = MyBase.GetDesignTimeHtml()
End If
Return designTimeHtml
End Function
End Class
End Namespace
注解
GetDesignTimeHtml如果不包含可显示字符,方法会将属性替换 Text 为 ID 控件的属性 Button Text 。The GetDesignTimeHtml method replaces the Text property with the ID property of the Button control if the Text contains no displayable characters. 然后,该 GetDesignTimeHtml 方法调用其基方法, ControlDesigner.GetDesignTimeHtml 该方法调用 Control.RenderControl 方法来生成标记。Then, the GetDesignTimeHtml method calls its base method, ControlDesigner.GetDesignTimeHtml, which calls into the Control.RenderControl method to generate the markup.
继承者说明
如果要重写 GetDesignTimeHtml() 方法,通常会修改所选的属性值,然后调用基方法来生成标记,然后将属性还原为其原始值。If you are overriding the GetDesignTimeHtml() method, typically you will modify selected property values, then call the base method to generate the markup, and then restore the properties to their original values.