Templated Custom Control

G

Gary K

Greetings & Salutations,

I have made a templated custom control (NOT data-bound), a container for the
templates & a designer for the control. After placing it on the form it
functions as expected, however I cannot edit the templates.

I have a Template Editing menu available that displays the templates I have
exposed, however when I click on a template to edit nothing happens.

I have found that the expected functions (GetTemplateContent &
CreateTemplateEditingFrame) do not get called at all. (I used
Debug.Assert(False) statements)

Since this is a pre-alpha control I have only done skeleton code for the
control/etc, but I followed the sample & I'm pretty sure that I have covered
all the main points as discussed in the help topics "Implementing a Web Forms
Template Editor" and "Web Forms Templated Data-Bound Control Designer Sample"
yet I cannot see what is going wrong.

Can anyone please help me?
 
G

Gary K

Ok, I've cleaned it up a bit, but it's still a bit messy. You should just be
able to cut-paste as a new file in an ASP.NET web application. PS:It's still
a pre-alpha, so please be gentle :)

---------------- Code Follows:
Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.Design
Imports System.Web.UI.WebControls

#Region "Template Designer"
Public Class Template1Designer
Inherits TemplatedControlDesigner

Private cvEditingVerbs() As TemplateEditingVerb = New
TemplateEditingVerb() { _
New TemplateEditingVerb("Header Template", 0, Me), _
New TemplateEditingVerb("Box Template", 1, Me)}

Public Overrides ReadOnly Property AllowResize() As Boolean
Get
Return True
End Get
End Property

Protected Overrides Function CreateTemplateEditingFrame(ByVal verb As
System.Web.UI.Design.TemplateEditingVerb) As
System.Web.UI.Design.ITemplateEditingFrame
Dim tn() As String = New String(1) {}
Dim es As ITemplateEditingService = Nothing

es = CType(GetService(GetType(ITemplateEditingService)),
ITemplateEditingService)
Select Case verb.Index
Case 0
tn(0) = "HeaderTemplate"
Return es.CreateFrame(Me, verb.Text, tn) ' worry about
styles when i get this bit working
Case 1
tn(0) = "BoxTemplate"
Return es.CreateFrame(Me, verb.Text, tn)
End Select
Return Nothing ' throw error?
End Function
Protected Overrides Function GetCachedTemplateEditingVerbs() As
System.Web.UI.Design.TemplateEditingVerb()
Return cvEditingVerbs
End Function
Public Overrides Function GetTemplateContent(ByVal editingFrame As
System.Web.UI.Design.ITemplateEditingFrame, ByVal templateName As String,
ByRef allowEditing As Boolean) As String
Try
Dim s As String = "Not valid verb[" & templateName & "](" &
editingFrame.Verb.Index & ")"

allowEditing = True
Select Case editingFrame.Verb.Index
Case 0
If templateName = "HeaderTemplate" Then
s = GetTextFromTemplate(CType(Component,
Template1).HeaderTemplate)
End If
Case 1
If templateName = "BoxTemplate" Then
s = GetTextFromTemplate(CType(Component,
Template1).BoxTemplate)
End If
End Select
Return s
Catch x As Exception
' actually catch errors in this proc
' it's easier to do that here then setup an error catching system
' (except now I know how to design-time debug, so this is useless)
Return "*ERROR*[" & x.Message & "](" & x.StackTrace & ")"
Catch
Return "*ERROR*"
End Try
End Function
Public Overrides Sub SetTemplateContent(ByVal editingFrame As
System.Web.UI.Design.ITemplateEditingFrame, ByVal templateName As String,
ByVal templateContent As String)
If Not (templateContent Is Nothing) AndAlso templateContent.Length >
0 Then
Select Case editingFrame.Verb.Index
Case 0
If templateName = "HeaderTemplate" Then
CType(Component, Template1).HeaderTemplate =
GetTemplateFromText(templateContent)
End If
Case 1
If templateName = "BoxTemplate" Then
CType(Component, Template1).BoxTemplate =
GetTemplateFromText(templateContent)
End If
End Select
End If
End Sub
End Class
#End Region

#Region "Template Container"
<DesignTimeVisible(False)> _
Public Class Template1Container
Inherits Control
Implements INamingContainer
' i know i can put a fair bit here, but for now
' it's just an empty control
End Class
#End Region

#Region "Templated Control"
<Designer(GetType(Template1Designer)), _
ParseChildren(True), _
ToolboxData("<{0}:Template1 runat=server></{0}:Template1>")> _
Public Class Template1
Inherits WebControl
Implements INamingContainer

Private cvHeaderTemplate As ITemplate
Private cvHeaderContainer As Template1Container
Private cvBoxTemplate As ITemplate
Private cvBoxContainer As Template1Container

<Browsable(False), _
TemplateContainer(GetType(Template1Container))> _
Public Property HeaderTemplate() As ITemplate
Get
Return cvHeaderTemplate
End Get
Set(ByVal Value As ITemplate)
cvHeaderTemplate = Value
End Set
End Property
<Browsable(False), _
TemplateContainer(GetType(Template1Container))> _
Public Property BoxTemplate() As ITemplate
Get
Return cvBoxTemplate
End Get
Set(ByVal Value As ITemplate)
cvBoxTemplate = Value
End Set
End Property

Public Sub New()
MyBase.New(HtmlTextWriterTag.Table)
End Sub

Protected Overrides Sub CreateChildControls()
If Not (cvHeaderTemplate Is Nothing) Then
cvHeaderContainer = New Template1Container
cvHeaderTemplate.InstantiateIn(cvHeaderContainer)
End If
If Not (cvHeaderTemplate Is Nothing) Then
cvBoxContainer = New Template1Container
cvBoxTemplate.InstantiateIn(cvBoxContainer)
End If
End Sub
Protected Overrides Sub OnDataBinding(ByVal e As System.EventArgs)
EnsureChildControls()
MyBase.OnDataBinding(e)
End Sub
Protected Overrides Sub Render(ByVal writer As
System.Web.UI.HtmlTextWriter)
EnsureChildControls()
RenderBeginTag(writer)
writer.Write("<tr>")
writer.Write("<td>")
If cvHeaderContainer Is Nothing Then
writer.Write("No Header Template [p]")
Else
cvHeaderContainer.RenderControl(writer)
End If
writer.WriteEndTag("td")
writer.WriteEndTag("tr")
writer.Write("<tr>")
writer.Write("<td>")
If cvBoxContainer Is Nothing Then
writer.Write("No Box Template")
Else
cvBoxContainer.RenderControl(writer)
End If
writer.WriteEndTag("td")
writer.WriteEndTag("tr")
RenderEndTag(writer)
End Sub
End Class
#End Region
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top