Professional Web Applications Themes

Custom Templated Control not rendering asp.net controls correctly - ASP.NET Building Controls

If I have an ASP.net button inside a template for a custom control and use the code below, everything works fine and it generates the correct id and the asp.net button calls serverside click event as it should: _myTemplateContainer = New ContentContainer(Me) ContentTemplate.InstantiateIn(_myTemplateContainer ) Controls.Add(_myTemplateContainer) But if I use this code, it does not generate the unique ID and no serverside event will be called: Dim SB As New StringBuilder() Dim SW As New StringWriter(SB) Dim htmlTW As New HtmlTextWriter(SW) _myTemplateContainer = New ContentContainer(Me) ContentTemplate.InstantiateIn(_myTemplateContainer ) _myTemplateContainer.RenderControl(htmlTW) If Not Me.Page.IsClientScriptBlockRegistered("MainWindow_ " & Me.ID) Then Me.Page.RegisterClientScriptBlock("MainWindow_" & Me.ID, SB.ToString) End If ...

  1. #1

    Default Custom Templated Control not rendering asp.net controls correctly

    If I have an ASP.net button inside a template for a custom control and use
    the code below, everything works fine and it generates the correct id and
    the asp.net button calls serverside click event as it should:

    _myTemplateContainer = New ContentContainer(Me)
    ContentTemplate.InstantiateIn(_myTemplateContainer )
    Controls.Add(_myTemplateContainer)

    But if I use this code, it does not generate the unique ID and no serverside
    event will be called:


    Dim SB As New StringBuilder()
    Dim SW As New StringWriter(SB)
    Dim htmlTW As New HtmlTextWriter(SW)
    _myTemplateContainer = New ContentContainer(Me)
    ContentTemplate.InstantiateIn(_myTemplateContainer )
    _myTemplateContainer.RenderControl(htmlTW)

    If Not Me.Page.IsClientScriptBlockRegistered("MainWindow_ " & Me.ID) Then
    Me.Page.RegisterClientScriptBlock("MainWindow_" & Me.ID, SB.ToString)
    End If

    The reason I was trying to do it this way is because I need the html of the
    template and some other html table code to be output to the top of the html
    page no matter where the actual custom control is sited and this was the
    only way I could figure out how to do that.

    Do you happen to know either of these answers:

    1. Is there something else I need to do to get RenderControl to generate
    proper ID's for asp.net control that exist on the template I am trying to
    render or is that just not possible.
    2. Is there another way I can make sure the output of the template always
    appears at the top of the page html?


    news.microsoft.com Guest

  2. #2

    Default Re: Custom Templated Control not rendering asp.net controls correctly

    Hi,

    on the second code block, you don't add the template container to the
    Controls collection, that's the key part in when Page generates IDs for
    controls or when events are handled (for postback events, control would need
    to be in Controls collection at Page_Load at the latest)

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU

    "news.microsoft.com" <kirk.quinbarzywave.com> wrote in message
    news:uFUUYeGPFHA.3076tk2msftngp13.phx.gbl...
    > If I have an ASP.net button inside a template for a custom control and use
    > the code below, everything works fine and it generates the correct id and
    > the asp.net button calls serverside click event as it should:
    >
    > _myTemplateContainer = New ContentContainer(Me)
    > ContentTemplate.InstantiateIn(_myTemplateContainer )
    > Controls.Add(_myTemplateContainer)
    >
    > But if I use this code, it does not generate the unique ID and no
    > serverside event will be called:
    >
    >
    > Dim SB As New StringBuilder()
    > Dim SW As New StringWriter(SB)
    > Dim htmlTW As New HtmlTextWriter(SW)
    > _myTemplateContainer = New ContentContainer(Me)
    > ContentTemplate.InstantiateIn(_myTemplateContainer )
    > _myTemplateContainer.RenderControl(htmlTW)
    >
    > If Not Me.Page.IsClientScriptBlockRegistered("MainWindow_ " & Me.ID) Then
    > Me.Page.RegisterClientScriptBlock("MainWindow_" & Me.ID, SB.ToString)
    > End If
    >
    > The reason I was trying to do it this way is because I need the html of
    > the template and some other html table code to be output to the top of the
    > html page no matter where the actual custom control is sited and this was
    > the only way I could figure out how to do that.
    >
    > Do you happen to know either of these answers:
    >
    > 1. Is there something else I need to do to get RenderControl to generate
    > proper ID's for asp.net control that exist on the template I am trying to
    > render or is that just not possible.
    > 2. Is there another way I can make sure the output of the template always
    > appears at the top of the page html?
    >
    >

    Teemu Keiski 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. Templated Custom Control
    By Gary K in forum ASP.NET Building Controls
    Replies: 2
    Last Post: June 9th, 11:09 PM
  3. templated control designers and default controls
    By Rick in forum ASP.NET Building Controls
    Replies: 1
    Last Post: October 1st, 08:21 PM
  4. 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
  5. 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

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