Implementing the ControlDesigner
Now for some action! Below is the code for the class, which will be used to provide HTML.
|Public Class DesignerHtml|
Public Overrides Function GetDesignTimeHTML() As String
Dim controltext() As String = CType(Component, DesignerSample).Text
If controltext.GetUpperBound(0) >= 1 Then
Dim writer As New System.IO.StringWriter()
Dim html As New HtmlTextWriter(writer)
Dim tbl As New HtmlTable()
tbl.Border = 1
Dim i As Integer
For i = 0 To controltext.GetUpperBound(0)
tr = New HtmlTableRow()
td = New HtmlTableCell()
td.InnerText = controltext(i)
You can see that this class simply inherits from ControlDesigner and overrides one of its functions - GetDesignTimeHTML().
The code for this function looks similar to the code for the actual render method for the Multi-Highlight control and it is. You can see that we use a StringWriter (to get our HTML as a string) and an HTMLTextWriter (to store our HTML) to hold and return the code.
We also use a series of HTML controls (HtmlTable, HtmlTableRow and HtmlTableCell) to build the table and then render it out.
You will notice that the PostBack code is missing (as described in this article), this is because the PostBack would not work well in a designer. The code provided here should only be a raw template or placeholder for the user to see the placement and possible size of the control (among other things), but it is not designed to provide total functionality of the control.
In the code I use - Dim controltext() as String = CType(Component, DesignerSample).Text - this gets the text of our Multi-Highlighter (this is in a class called DesignerSample in this example). The Component (in the code) is an instance of the control (or component) that this designer is associated with (as you will see later).
Finally, you will notice that I use GetEmptyDesignTimeHTML() without overriding it, this will provide the default HTML (nothing).