Implementing the ControlDesigner
Now for some action! Below is the code for the class, which will be used to provide HTML.
Public Class DesignerHtml Inherits System.Web.UI.Design.ControlDesigner
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 tbl.Style.Add("cursor", "hand") tbl.Style.Add("border-width", "1") tbl.Style.Add("border-color", "#000000") Dim tr Dim td Dim i As Integer For i = 0 To controltext.GetUpperBound(0) tr = New HtmlTableRow() td = New HtmlTableCell() tr.Style.Add("bgcolor", "#FFFFFF") td.InnerText = controltext(i) tr.Controls.Add(td) tbl.Controls.Add(tr) Next tbl.RenderControl(html) Return writer.ToString() Else Return GetEmptyDesignTimeHtml() End If
End Function End Class |
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).