Custom Control Designers : Design-time HTML
page 4 of 8
by . .
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 34107/ 42

Implementing the ControlDesigner

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
controltext() As String = CType(Component, DesignerSample).Text

controltext.GetUpperBound(0) >= 1 Then
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
i = 0 To controltext.GetUpperBound(0)
tr = New HtmlTableRow()
td = New HtmlTableCell()
tr.Style.Add("bgcolor", "#FFFFFF")
td.InnerText = controltext(i)
Return writer.ToString()
End If

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).

View Entire Article

User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-06-17 7:11:17 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search