Nesting WebControls in CustomControl

nju0843

Member
Joined
Jul 7, 2005
Messages
16
Location
Lafayette, LA
Programming Experience
5-10
Hello All!

I was wondering if you knew of a way to allow nesting of
a webcontrol inside of the custom control I am creating.
For instance...

HTML:
<cc1:ControlFrame>
     <asp:Label text="Keyword 1:"/>
     <asp:TextBox id="txtKeyword1" runat="server"/>
</cc1:ControlFrame>
For the html above the two ASP tags get stripped out when
parsed...

The html below gets parsed and displayed to the page perfectly.

HTML:
 <cc1:ControlFrame>
      <span>Keyword 1:</span>
      <input id="txtKeyword1" type="text">
 </cc1:ControlFrame>
For some reason the asp tags get parsed differently and it does not
get handled...I would like to know
1) Why this happens...
2) Is there a solution for it?

All of the code for my CustomControl is posted below...

VB.NET:
Imports System.ComponentModel
Imports System.Web.UI

Public Class MyControlBuilder
    Inherits ControlBuilder

    Public _parser As System.Web.UI.TemplateParser
    Public _parentBuilder As System.Web.UI.ControlBuilder
    Public _type As System.Type
    Public _tagName As System.String
    Public _id As System.String
    Public _attribs As System.Collections.IDictionary

    Public Overrides Sub Init(ByVal parser As System.Web.UI.TemplateParser, ByVal parentBuilder As System.Web.UI.ControlBuilder, ByVal type As System.Type, ByVal tagName As System.String, ByVal id As System.String, ByVal attribs As System.Collections.IDictionary)

        _parser = parser
        _parentBuilder = parentBuilder
        _type = type
        _tagName = tagName
        _id = id
        _attribs = attribs

    End Sub

    Public Overrides Sub AppendLiteralString(ByVal s As String)
        s = s.Trim
        _attribs("Text") = s
        MyBase.Init(_parser, _parentBuilder, _type, _tagName, _id, _attribs)
    End Sub

End Class

<DefaultProperty("Text"), _
 PersistChildren(True), _
 ControlBuilderAttribute(GetType(MyControlBuilder)), _
 ParseChildren(False), _
 ToolboxData("<{0}:MyDirectoryFrames runat=server></{0}:MyDirectoryFrames>")> _
Public Class MyDirectoryFrames
    Inherits System.Web.UI.WebControls.WebControl

    Dim _text As String
    Dim _title As String
    Dim _InnerHtml As String

    <Bindable(True), Category("Appearance"), DefaultValue("")> Property [Text]() As String
        Get
            Return _text
        End Get

        Set(ByVal Value As String)
            _text = Value
        End Set
    End Property

    <Bindable(False), Category(), DefaultValue("")> Property [Title]() As String
        Get
            Return _title
        End Get
        Set(ByVal Value As String)
            _title = Value
        End Set
    End Property

    Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)
        output.WriteLine("<table class=""sTableInner"" id=""tdheader"">")
        output.WriteLine("  <tr>")
        output.WriteLine("      <td class=""sTableTopLeft""><img src=""http://localhost/My_Directory/my_images/mycontrols/spacer.gif"" height=""7"" width=""7""></td>")
        output.WriteLine("      <td class=""sTableTopMid"" width=""100%""></td>")
        output.WriteLine("      <td class=""sTableTopRight""><img src=""http://localhost/my_Directory/my_images/mycontrols/spacer.gif"" height=""7"" width=""7""></td>")
        output.WriteLine("  </tr>")
        output.WriteLine("  <tr>")
        output.WriteLine("      <td class=""sTableMidLeft""></td>")
        output.WriteLine("      <td class=""sTableMid"">")
        output.WriteLine("          <table width=""100%"">")
        output.WriteLine("              <tr class=""sGridColumnHeader"">")
        output.WriteLine("                  <td width=""100%"" onclick=""javascript:toggleSearch();"">" + [Title] + "</td>")
        output.WriteLine("              </tr>")
        output.WriteLine("          </table>")
        output.WriteLine("      </td>")
        output.WriteLine("      <td class=""sTableMidRight""></td>")
        output.WriteLine("  </tr>")
        output.WriteLine("  <tr height=""100%"" valign=""top"">")
        output.WriteLine("      <td class=""wTableMidLeft""></td>")
        output.WriteLine("      <td class=""wTableMid"">")
        output.Write([Text])
        output.WriteLine("      </td>")
        output.WriteLine("        <td class=""wTableMidRight""></td>")
        output.WriteLine("  </tr>")
        output.WriteLine("    <tr>")
        output.WriteLine("        <td class=""wTableBottomLeft""><img src=""http://localhost/my_Directory/my_images/mycontrols/spacer.gif"" height=""7"" width=""7""></td>")
        output.WriteLine("        <td class=""wTableBottomMid""></td>")
        output.WriteLine("        <td class=""wTableBottomRight""><img src=""http://localhost/my_Directory/my_images/mycontrols/spacer.gif"" height=""7"" width=""7""></td>")
        output.WriteLine("    </tr>")
        output.WriteLine("</table>")
    End Sub

End Class
Thanks so much for your help...
-nju0843
 
Anyone have an idea?

If anyone has any idea on how to do this please let me know...
Even a suggestion would be nice...
I was messing around with the HTMLTextWriter and the Write function completely removes any trace of an <asp:Control> tag...

I just wanted to let you guys know that I was still stumped and have been checking back for answers everyday.
 
I do this dynamically... I put a placeholder in my usercontrol and then on the page_prerender event I dynamically load controls, set the props and add it to the placeholder. if you want the controls to be persisted across postback use the DBauer placeholder (free on the internet).

If you want to pre-type the nested control inside another control, I know this is possible and it is done w/a template... will need to do more research to remember exactly how I did it that way.

You may read this article on templates:
http://msdn.microsoft.com/msdnmag/issues/02/01/cutting/

It looks like ItemTemplate is the way to go?
 
Back
Top