Custom Templated Control not rendering asp.net controls correctly

Discussion in 'ASP .Net Building Controls' started by news.microsoft.com, Apr 8, 2005.

  1. 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, Apr 8, 2005
    #1
    1. Advertising

  2. news.microsoft.com

    Teemu Keiski Guest

    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" <> wrote in message
    news:...
    > 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, Apr 9, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.microsoft.com
    Replies:
    1
    Views:
    558
    Teemu Keiski
    Apr 9, 2005
  2. Replies:
    0
    Views:
    2,286
  3. news.microsoft.com

    Custom Templated Control not rendering asp.net controls correctly

    news.microsoft.com, Apr 8, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    171
    Teemu Keiski
    Apr 9, 2005
  4. Allan Ebdrup

    Templated control not rendering Web User Controls

    Allan Ebdrup, Mar 27, 2006, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    245
    Steven Cheng[MSFT]
    Mar 30, 2006
  5. Robin9876

    Calendar Control not rendering correctly

    Robin9876, Aug 1, 2007, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    133
    Robin9876
    Aug 1, 2007
Loading...

Share This Page