Custom Control with Child Controls

Discussion in 'ASP .Net Building Controls' started by Nitin, Oct 11, 2003.

  1. Nitin

    Nitin Guest

    Hi,

    I have created 2 custom control : MyControl and C1
    I have following tags in my aspx file :
    <myCtrl:MyControl runat=server>
    <myCtrl:C1 Text="A" runat=server />
    <myCtrl:C1 Text="B" runat=server />
    </myCtrl:MyControl>

    Now I want to render the output in the following manner :
    <TABLE>
    <TR>
    <TD>A</TD>
    </TR>
    <TR>
    <TD>B</TD>
    </TR>
    </TABLE>

    Custom control C1 will render each row (<tr>) which I am
    able to do by having a property named "Text" inside C1 and
    overriding the Render method.

    Custom control MyControl should start a Table and also end
    the table when all the child controls are rendered.

    I am not able to close the table after all the child
    controls are rendered.
    I tried having a Custom ControlBuilder Class and
    overriding some methods but that is also not working.

    This thing works with asp.net controls such as :

    <asp:LinkButton ID="lb1" Runat=server>
    LinkButton <br>
    <asp:Label ID="l1" Runat=server>one</asp:Label>
    <br>
    <asp:Label ID="l2" Runat=server>two</asp:Label>
    <br>
    End
    </asp:LinkButton>

    The output when I see source :

    <a id="lb1" href="javascript:__doPostBack('lb1','')">
    LinkButton <br>
    <span id="l1">one</span><br>
    <span id="l2">two</span><br>
    End
    </a>

    My problem in my control is how to get this last "</a>"
    tag.

    Kindly help.

    Regards,
    Nitin
     
    Nitin, Oct 11, 2003
    #1
    1. Advertising

  2. Nitin

    Teemu Keiski Guest

    How have you implemented the controls?

    Does MyControl allow child controls (i.e is it derived from Control or
    WebControl and if WebControl then has it design-time attributes applid
    namely ParseChildren(false) and PersistChildren(true) so that it supports
    content between its tags as child controls)

    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist

    "Nitin" <> wrote in message
    news:080901c38fe6$5171d310$...
    > Hi,
    >
    > I have created 2 custom control : MyControl and C1
    > I have following tags in my aspx file :
    > <myCtrl:MyControl runat=server>
    > <myCtrl:C1 Text="A" runat=server />
    > <myCtrl:C1 Text="B" runat=server />
    > </myCtrl:MyControl>
    >
    > Now I want to render the output in the following manner :
    > <TABLE>
    > <TR>
    > <TD>A</TD>
    > </TR>
    > <TR>
    > <TD>B</TD>
    > </TR>
    > </TABLE>
    >
    > Custom control C1 will render each row (<tr>) which I am
    > able to do by having a property named "Text" inside C1 and
    > overriding the Render method.
    >
    > Custom control MyControl should start a Table and also end
    > the table when all the child controls are rendered.
    >
    > I am not able to close the table after all the child
    > controls are rendered.
    > I tried having a Custom ControlBuilder Class and
    > overriding some methods but that is also not working.
    >
    > This thing works with asp.net controls such as :
    >
    > <asp:LinkButton ID="lb1" Runat=server>
    > LinkButton <br>
    > <asp:Label ID="l1" Runat=server>one</asp:Label>
    > <br>
    > <asp:Label ID="l2" Runat=server>two</asp:Label>
    > <br>
    > End
    > </asp:LinkButton>
    >
    > The output when I see source :
    >
    > <a id="lb1" href="javascript:__doPostBack('lb1','')">
    > LinkButton <br>
    > <span id="l1">one</span><br>
    > <span id="l2">two</span><br>
    > End
    > </a>
    >
    > My problem in my control is how to get this last "</a>"
    > tag.
    >
    > Kindly help.
    >
    > Regards,
    > Nitin
    >
     
    Teemu Keiski, Oct 11, 2003
    #2
    1. Advertising

  3. Nitin

    Nitin Guest

    Hi Teemu,

    Here is my code for MyControl and C1

    using System;
    using System.Web;
    using System.Data;
    using System.Xml;
    using System.Text;
    using System.Collections;
    using System.Diagnostics;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;
    using System.Runtime.Serialization;

    namespace Controls.Common
    {
    /// <summary>
    /// Summary description for MyControl.
    /// </summary>
    public class MyControl : Control
    {
    public MyControl()
    {
    }

    #region Properties
    private string _HeaderText;
    public string HeaderText
    {
    get
    {
    return _HeaderText;
    }
    set
    {
    _HeaderText = value;
    }
    }
    #endregion
    }
    }


    using System;
    using System.Web;
    using System.Data;
    using System.Xml;
    using System.Text;
    using System.Collections;
    using System.Diagnostics;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;
    using System.Runtime.Serialization;

    namespace Controls.Common
    {
    /// <summary>
    /// Summary description for C1.
    /// </summary>
    public class C1 : Control
    {
    public C1()
    {
    }

    #region Properties
    private string _Text;
    public string Text
    {
    get
    {
    return _Text;
    }
    set
    {
    _Text = value;
    }
    }
    #endregion

    #region Protected Methods
    protected override void Render
    (HtmlTextWriter output)
    {
    output.Write("<tr>");
    output.Write("<td>");
    output.Write("<A HREF=\"#\" id=\"A1
    \" onclick=\"return f_Change('A1');\" CLASS=\"Link\">");
    output.Write(this._Text);
    output.Write("</A>");
    output.Write("</td>");
    output.Write("</tr>");
    }
    #endregion

    }
    }


    and aspx file code :

    <%@ Register TagPrefix="myCtrl"
    Namespace="Controls.Common" Assembly="ControlLibrary" %>


    <form id="TestMyControl" method="post" runat="server">
    <myCtrl:MyControl runat=server >
    <myCtrl:C1 Text="A" runat=server />
    <myCtrl:C1 Text="B" runat=server />
    </myCtrl:MyControl>
    </form>


    and output I get is (view source):


    <form name="TestMyControl" method="post"
    action="TestMyControl.aspx" id="TestMyControl">
    <input type="hidden" name="__VIEWSTATE"
    value="dDwxNTc2NTg4MTgzOzs+VpGy0tGPOz32SgraT7FLb4yXk7s=" />

    <tr><td><A HREF="#" id="A1" onclick="return f_Change
    ('A1');" CLASS="Link">A</A></td></tr>
    <tr><td><A HREF="#" id="A1" onclick="return f_Change
    ('A1');" CLASS="Link">B</A></td></tr>

    </form>



    I just want my Parent control (MyControl) to start a table
    tag and close a table tag. I mean the parent control
    should start some html tags and after all the child
    controls have finished rendering then the parent control
    shouls close all the tags which it started.


    Kindly guide me how to do this.

    Regards,
    Nitin



    >-----Original Message-----
    >How have you implemented the controls?
    >
    >Does MyControl allow child controls (i.e is it derived

    from Control or
    >WebControl and if WebControl then has it design-time

    attributes applid
    >namely ParseChildren(false) and PersistChildren(true) so

    that it supports
    >content between its tags as child controls)
    >
    >--
    >Teemu Keiski
    >MCP, Microsoft MVP (ASP.NET), AspInsiders member
    >ASP.NET Forum Moderator, AspAlliance Columnist
    >
    >"Nitin" <> wrote in message
    >news:080901c38fe6$5171d310$...
    >> Hi,
    >>
    >> I have created 2 custom control : MyControl and C1
    >> I have following tags in my aspx file :
    >> <myCtrl:MyControl runat=server>
    >> <myCtrl:C1 Text="A" runat=server />
    >> <myCtrl:C1 Text="B" runat=server />
    >> </myCtrl:MyControl>
    >>
    >> Now I want to render the output in the following

    manner :
    >> <TABLE>
    >> <TR>
    >> <TD>A</TD>
    >> </TR>
    >> <TR>
    >> <TD>B</TD>
    >> </TR>
    >> </TABLE>
    >>
    >> Custom control C1 will render each row (<tr>) which I am
    >> able to do by having a property named "Text" inside C1

    and
    >> overriding the Render method.
    >>
    >> Custom control MyControl should start a Table and also

    end
    >> the table when all the child controls are rendered.
    >>
    >> I am not able to close the table after all the child
    >> controls are rendered.
    >> I tried having a Custom ControlBuilder Class and
    >> overriding some methods but that is also not working.
    >>
    >> This thing works with asp.net controls such as :
    >>
    >> <asp:LinkButton ID="lb1" Runat=server>
    >> LinkButton <br>
    >> <asp:Label ID="l1" Runat=server>one</asp:Label>
    >> <br>
    >> <asp:Label ID="l2" Runat=server>two</asp:Label>
    >> <br>
    >> End
    >> </asp:LinkButton>
    >>
    >> The output when I see source :
    >>
    >> <a id="lb1" href="javascript:__doPostBack('lb1','')">
    >> LinkButton <br>
    >> <span id="l1">one</span><br>
    >> <span id="l2">two</span><br>
    >> End
    >> </a>
    >>
    >> My problem in my control is how to get this last "</a>"
    >> tag.
    >>
    >> Kindly help.
    >>
    >> Regards,
    >> Nitin
    >>

    >
    >
    >.
    >
     
    Nitin, Oct 12, 2003
    #3
  4. Nitin

    Teemu Keiski Guest

    In the MyControl's Render call RenderChildren there where you want child
    controls to be rendered.

    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist


    "Nitin" <> wrote in message
    news:043e01c39097$f2057050$...
    > Hi Teemu,
    >
    > Here is my code for MyControl and C1
    >
    > using System;
    > using System.Web;
    > using System.Data;
    > using System.Xml;
    > using System.Text;
    > using System.Collections;
    > using System.Diagnostics;
    > using System.Web.UI;
    > using System.Web.UI.WebControls;
    > using System.ComponentModel;
    > using System.Runtime.Serialization;
    >
    > namespace Controls.Common
    > {
    > /// <summary>
    > /// Summary description for MyControl.
    > /// </summary>
    > public class MyControl : Control
    > {
    > public MyControl()
    > {
    > }
    >
    > #region Properties
    > private string _HeaderText;
    > public string HeaderText
    > {
    > get
    > {
    > return _HeaderText;
    > }
    > set
    > {
    > _HeaderText = value;
    > }
    > }
    > #endregion
    > }
    > }
    >
    >
    > using System;
    > using System.Web;
    > using System.Data;
    > using System.Xml;
    > using System.Text;
    > using System.Collections;
    > using System.Diagnostics;
    > using System.Web.UI;
    > using System.Web.UI.WebControls;
    > using System.ComponentModel;
    > using System.Runtime.Serialization;
    >
    > namespace Controls.Common
    > {
    > /// <summary>
    > /// Summary description for C1.
    > /// </summary>
    > public class C1 : Control
    > {
    > public C1()
    > {
    > }
    >
    > #region Properties
    > private string _Text;
    > public string Text
    > {
    > get
    > {
    > return _Text;
    > }
    > set
    > {
    > _Text = value;
    > }
    > }
    > #endregion
    >
    > #region Protected Methods
    > protected override void Render
    > (HtmlTextWriter output)
    > {
    > output.Write("<tr>");
    > output.Write("<td>");
    > output.Write("<A HREF=\"#\" id=\"A1
    > \" onclick=\"return f_Change('A1');\" CLASS=\"Link\">");
    > output.Write(this._Text);
    > output.Write("</A>");
    > output.Write("</td>");
    > output.Write("</tr>");
    > }
    > #endregion
    >
    > }
    > }
    >
    >
    > and aspx file code :
    >
    > <%@ Register TagPrefix="myCtrl"
    > Namespace="Controls.Common" Assembly="ControlLibrary" %>
    >
    >
    > <form id="TestMyControl" method="post" runat="server">
    > <myCtrl:MyControl runat=server >
    > <myCtrl:C1 Text="A" runat=server />
    > <myCtrl:C1 Text="B" runat=server />
    > </myCtrl:MyControl>
    > </form>
    >
    >
    > and output I get is (view source):
    >
    >
    > <form name="TestMyControl" method="post"
    > action="TestMyControl.aspx" id="TestMyControl">
    > <input type="hidden" name="__VIEWSTATE"
    > value="dDwxNTc2NTg4MTgzOzs+VpGy0tGPOz32SgraT7FLb4yXk7s=" />
    >
    > <tr><td><A HREF="#" id="A1" onclick="return f_Change
    > ('A1');" CLASS="Link">A</A></td></tr>
    > <tr><td><A HREF="#" id="A1" onclick="return f_Change
    > ('A1');" CLASS="Link">B</A></td></tr>
    >
    > </form>
    >
    >
    >
    > I just want my Parent control (MyControl) to start a table
    > tag and close a table tag. I mean the parent control
    > should start some html tags and after all the child
    > controls have finished rendering then the parent control
    > shouls close all the tags which it started.
    >
    >
    > Kindly guide me how to do this.
    >
    > Regards,
    > Nitin
    >
    >
    >
    > >-----Original Message-----
    > >How have you implemented the controls?
    > >
    > >Does MyControl allow child controls (i.e is it derived

    > from Control or
    > >WebControl and if WebControl then has it design-time

    > attributes applid
    > >namely ParseChildren(false) and PersistChildren(true) so

    > that it supports
    > >content between its tags as child controls)
    > >
    > >--
    > >Teemu Keiski
    > >MCP, Microsoft MVP (ASP.NET), AspInsiders member
    > >ASP.NET Forum Moderator, AspAlliance Columnist
    > >
    > >"Nitin" <> wrote in message
    > >news:080901c38fe6$5171d310$...
    > >> Hi,
    > >>
    > >> I have created 2 custom control : MyControl and C1
    > >> I have following tags in my aspx file :
    > >> <myCtrl:MyControl runat=server>
    > >> <myCtrl:C1 Text="A" runat=server />
    > >> <myCtrl:C1 Text="B" runat=server />
    > >> </myCtrl:MyControl>
    > >>
    > >> Now I want to render the output in the following

    > manner :
    > >> <TABLE>
    > >> <TR>
    > >> <TD>A</TD>
    > >> </TR>
    > >> <TR>
    > >> <TD>B</TD>
    > >> </TR>
    > >> </TABLE>
    > >>
    > >> Custom control C1 will render each row (<tr>) which I am
    > >> able to do by having a property named "Text" inside C1

    > and
    > >> overriding the Render method.
    > >>
    > >> Custom control MyControl should start a Table and also

    > end
    > >> the table when all the child controls are rendered.
    > >>
    > >> I am not able to close the table after all the child
    > >> controls are rendered.
    > >> I tried having a Custom ControlBuilder Class and
    > >> overriding some methods but that is also not working.
    > >>
    > >> This thing works with asp.net controls such as :
    > >>
    > >> <asp:LinkButton ID="lb1" Runat=server>
    > >> LinkButton <br>
    > >> <asp:Label ID="l1" Runat=server>one</asp:Label>
    > >> <br>
    > >> <asp:Label ID="l2" Runat=server>two</asp:Label>
    > >> <br>
    > >> End
    > >> </asp:LinkButton>
    > >>
    > >> The output when I see source :
    > >>
    > >> <a id="lb1" href="javascript:__doPostBack('lb1','')">
    > >> LinkButton <br>
    > >> <span id="l1">one</span><br>
    > >> <span id="l2">two</span><br>
    > >> End
    > >> </a>
    > >>
    > >> My problem in my control is how to get this last "</a>"
    > >> tag.
    > >>
    > >> Kindly help.
    > >>
    > >> Regards,
    > >> Nitin
    > >>

    > >
    > >
    > >.
    > >
     
    Teemu Keiski, Oct 12, 2003
    #4
  5. Nitin

    Nitin. Guest

    Hi Teemu,

    Thanks a lot for this quick reply. It worked with
    RenderChildren.

    Regards,
    Nitin

    >-----Original Message-----
    >In the MyControl's Render call RenderChildren there where

    you want child
    >controls to be rendered.
    >
    >--
    >Teemu Keiski
    >MCP, Microsoft MVP (ASP.NET), AspInsiders member
    >ASP.NET Forum Moderator, AspAlliance Columnist
    >
    >
    >"Nitin" <> wrote in message
    >news:043e01c39097$f2057050$...
    >> Hi Teemu,
    >>
    >> Here is my code for MyControl and C1
    >>
    >> using System;
    >> using System.Web;
    >> using System.Data;
    >> using System.Xml;
    >> using System.Text;
    >> using System.Collections;
    >> using System.Diagnostics;
    >> using System.Web.UI;
    >> using System.Web.UI.WebControls;
    >> using System.ComponentModel;
    >> using System.Runtime.Serialization;
    >>
    >> namespace Controls.Common
    >> {
    >> /// <summary>
    >> /// Summary description for MyControl.
    >> /// </summary>
    >> public class MyControl : Control
    >> {
    >> public MyControl()
    >> {
    >> }
    >>
    >> #region Properties
    >> private string _HeaderText;
    >> public string HeaderText
    >> {
    >> get
    >> {
    >> return _HeaderText;
    >> }
    >> set
    >> {
    >> _HeaderText = value;
    >> }
    >> }
    >> #endregion
    >> }
    >> }
    >>
    >>
    >> using System;
    >> using System.Web;
    >> using System.Data;
    >> using System.Xml;
    >> using System.Text;
    >> using System.Collections;
    >> using System.Diagnostics;
    >> using System.Web.UI;
    >> using System.Web.UI.WebControls;
    >> using System.ComponentModel;
    >> using System.Runtime.Serialization;
    >>
    >> namespace Controls.Common
    >> {
    >> /// <summary>
    >> /// Summary description for C1.
    >> /// </summary>
    >> public class C1 : Control
    >> {
    >> public C1()
    >> {
    >> }
    >>
    >> #region Properties
    >> private string _Text;
    >> public string Text
    >> {
    >> get
    >> {
    >> return _Text;
    >> }
    >> set
    >> {
    >> _Text = value;
    >> }
    >> }
    >> #endregion
    >>
    >> #region Protected Methods
    >> protected override void Render
    >> (HtmlTextWriter output)
    >> {
    >> output.Write("<tr>");
    >> output.Write("<td>");
    >> output.Write("<A HREF=\"#\" id=\"A1
    >> \" onclick=\"return f_Change('A1');\" CLASS=\"Link\">");
    >> output.Write(this._Text);
    >> output.Write("</A>");
    >> output.Write("</td>");
    >> output.Write("</tr>");
    >> }
    >> #endregion
    >>
    >> }
    >> }
    >>
    >>
    >> and aspx file code :
    >>
    >> <%@ Register TagPrefix="myCtrl"
    >> Namespace="Controls.Common" Assembly="ControlLibrary" %>
    >>
    >>
    >> <form id="TestMyControl" method="post" runat="server">
    >> <myCtrl:MyControl runat=server >
    >> <myCtrl:C1 Text="A" runat=server />
    >> <myCtrl:C1 Text="B" runat=server />
    >> </myCtrl:MyControl>
    >> </form>
    >>
    >>
    >> and output I get is (view source):
    >>
    >>
    >> <form name="TestMyControl" method="post"
    >> action="TestMyControl.aspx" id="TestMyControl">
    >> <input type="hidden" name="__VIEWSTATE"
    >>

    value="dDwxNTc2NTg4MTgzOzs+VpGy0tGPOz32SgraT7FLb4yXk7s=" />
    >>
    >> <tr><td><A HREF="#" id="A1" onclick="return f_Change
    >> ('A1');" CLASS="Link">A</A></td></tr>
    >> <tr><td><A HREF="#" id="A1" onclick="return f_Change
    >> ('A1');" CLASS="Link">B</A></td></tr>
    >>
    >> </form>
    >>
    >>
    >>
    >> I just want my Parent control (MyControl) to start a

    table
    >> tag and close a table tag. I mean the parent control
    >> should start some html tags and after all the child
    >> controls have finished rendering then the parent control
    >> shouls close all the tags which it started.
    >>
    >>
    >> Kindly guide me how to do this.
    >>
    >> Regards,
    >> Nitin
    >>
    >>
    >>
    >> >-----Original Message-----
    >> >How have you implemented the controls?
    >> >
    >> >Does MyControl allow child controls (i.e is it derived

    >> from Control or
    >> >WebControl and if WebControl then has it design-time

    >> attributes applid
    >> >namely ParseChildren(false) and PersistChildren(true)

    so
    >> that it supports
    >> >content between its tags as child controls)
    >> >
    >> >--
    >> >Teemu Keiski
    >> >MCP, Microsoft MVP (ASP.NET), AspInsiders member
    >> >ASP.NET Forum Moderator, AspAlliance Columnist
    >> >
    >> >"Nitin" <> wrote in message
    >> >news:080901c38fe6$5171d310$...
    >> >> Hi,
    >> >>
    >> >> I have created 2 custom control : MyControl and C1
    >> >> I have following tags in my aspx file :
    >> >> <myCtrl:MyControl runat=server>
    >> >> <myCtrl:C1 Text="A" runat=server />
    >> >> <myCtrl:C1 Text="B" runat=server />
    >> >> </myCtrl:MyControl>
    >> >>
    >> >> Now I want to render the output in the following

    >> manner :
    >> >> <TABLE>
    >> >> <TR>
    >> >> <TD>A</TD>
    >> >> </TR>
    >> >> <TR>
    >> >> <TD>B</TD>
    >> >> </TR>
    >> >> </TABLE>
    >> >>
    >> >> Custom control C1 will render each row (<tr>) which

    I am
    >> >> able to do by having a property named "Text" inside

    C1
    >> and
    >> >> overriding the Render method.
    >> >>
    >> >> Custom control MyControl should start a Table and

    also
    >> end
    >> >> the table when all the child controls are rendered.
    >> >>
    >> >> I am not able to close the table after all the child
    >> >> controls are rendered.
    >> >> I tried having a Custom ControlBuilder Class and
    >> >> overriding some methods but that is also not working.
    >> >>
    >> >> This thing works with asp.net controls such as :
    >> >>
    >> >> <asp:LinkButton ID="lb1" Runat=server>
    >> >> LinkButton <br>
    >> >> <asp:Label ID="l1" Runat=server>one</asp:Label>
    >> >> <br>
    >> >> <asp:Label ID="l2" Runat=server>two</asp:Label>
    >> >> <br>
    >> >> End
    >> >> </asp:LinkButton>
    >> >>
    >> >> The output when I see source :
    >> >>
    >> >> <a id="lb1" href="javascript:__doPostBack('lb1','')">
    >> >> LinkButton <br>
    >> >> <span id="l1">one</span><br>
    >> >> <span id="l2">two</span><br>
    >> >> End
    >> >> </a>
    >> >>
    >> >> My problem in my control is how to get this

    last "</a>"
    >> >> tag.
    >> >>
    >> >> Kindly help.
    >> >>
    >> >> Regards,
    >> >> Nitin
    >> >>
    >> >
    >> >
    >> >.
    >> >

    >
    >
    >.
    >
     
    Nitin., Oct 12, 2003
    #5
    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. Jeff Rodriguez
    Replies:
    23
    Views:
    1,132
    David Schwartz
    Dec 9, 2003
  2. Chad Scharf
    Replies:
    3
    Views:
    893
    Chad Scharf
    Oct 18, 2007
  3. Replies:
    0
    Views:
    208
  4. Replies:
    1
    Views:
    181
    Teemu Keiski
    Dec 8, 2005
  5. Replies:
    1
    Views:
    191
    Teemu Keiski
    Dec 8, 2005
Loading...

Share This Page