Professional Web Applications Themes

Templated Custom Control - ASP.NET Building Controls

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

  1. #1

    Default Templated Custom Control

    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?
    Gary K Guest

  2. #2

    Default Re: Templated Custom Control

    Could you share with us the code?
    Thanks
    On Tue, 07 Jun 2005 23:31:03 -0700, Gary
    K wrote:
    > 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?
    intrader Guest

  3. #3

    Default Re: Templated Custom Control

    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)), _
    PChildren(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
    Gary K Guest

Similar Threads

  1. Custom Templated Databound Control or derived control?
    By MattC in forum ASP.NET Building Controls
    Replies: 1
    Last Post: November 25th, 03:16 PM
  2. Custom Templated Control not rendering asp.net controls correctly
    By news.microsoft.com in forum ASP.NET Building Controls
    Replies: 1
    Last Post: April 9th, 11:41 AM
  3. Embeded Server Controls in a Custom Templated Control
    By James Hawes in forum ASP.NET Building Controls
    Replies: 1
    Last Post: September 29th, 04:08 PM
  4. Deleting controls at designtime in web Custom templated control
    By Alessandro Zifiglio in forum ASP.NET Building Controls
    Replies: 0
    Last Post: December 13th, 02:09 PM
  5. attempt to create my own templated custom control
    By Lloyd Dupont in forum ASP.NET General
    Replies: 0
    Last Post: July 27th, 06:50 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139