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

Making it Dynamic

Making it Dynamic

So far, this code has produced a static sample. The reason is that the code for setting the Text property is set in the Page_Load method because it is an array. Here, we will add it to the Property Inspector so you can enter an array, change the code to be dynamic and make a static version.

We do not need to make any modifications to our DesignerSample class because we already made them. It is important to remember that because we're dealing with an array, we must initialize it or we will get an error in VS.NET.

Now, I want to make it dynamic, but just incase they don't enter anything into the Text property, we will have a back up - our current code.
 

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

Protected
Overrides Function GetEmptyDesignTimeHtml() As String
Dim controltext() As String = {"[Sample 1]", "[Sample 2]", "[Sample 3]"}

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()
End Function

End Class

All that changed was that I removed the If statement and it now overrides GetEmptyDesignTimeHTML() instead of GetDesigntimeHTML().

Also we shifted the array back to it's previous way (of being dynamic) while providing a safety net if it does not work out as planned.


View Entire Article

User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





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


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-10-12 11:31:35 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search