Building a custom control that implement table server control

Discussion in 'ASP .Net Building Controls' started by Thomas Ekegren, Jul 19, 2003.

  1. Hi,

    I want to create a custom control that implements the table htmlcontrol. In
    my webform page I want to include that custom control and add content to a
    cell in my custom table control. I don't want to do this dynamically in the
    page_load event but in the HTML design view.

    Example:

    My custom control containes
    a table
    one row
    and three cells

    _____________________________
    | Cell1 | Cell2 | Cell3 |
    _____________________________

    I want to position my content in cell #2

    <HTML>
    <BODY>
    <CustomControl:myTable id="myTable1" runat="server">

    /**** this is what i want to position in cell #2 ****/
    <tr>
    <td>My text</td>
    </tr>

    </CustomControl:myTable>
    </BODY>
    </HTML>

    Any suggestion as to how to accomplish this?

    Thanks,

    /ekegren
     
    Thomas Ekegren, Jul 19, 2003
    #1
    1. Advertising

  2. Hi Thomas,

    You could write a composite control that contains an HtmlTable control. This
    composite control would allow the user to declaratively specify its child
    controls, ie:

    <cc:YourCompositeControl>
    //contents for 2nd cell goes here
    </cc:YourCompositeControl>

    Then your composite control should create an HtmlTable, add one row and
    three cells, and insert the specified child controls in the 2nd row.

    You can find more info about composite controls and their keys method (ie.
    CreateChildControls, etc) in the docs and in web tutorials. You will need
    also to get familiar with attributes and the specific ones that control the
    parsing and serialization of your control and its childs
    (ParseChildrenAttribute, etc). There are lots of posts and samples posted in
    this ng, you could use http://groups.google.com to find them and learn from
    there.

    Give that a try and post here any problems you encounter,


    --
    Victor Garcia Aprea
    Microsoft MVP | ASP.NET
    Looking for insights on ASP.NET? Read my blog:
    http://obies.com/vga/blog.aspx
    To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
    and not by private mail.

    "Thomas Ekegren" <> wrote in message
    news:...
    > Hi,
    >
    > I want to create a custom control that implements the table htmlcontrol.

    In
    > my webform page I want to include that custom control and add content to a
    > cell in my custom table control. I don't want to do this dynamically in

    the
    > page_load event but in the HTML design view.
    >
    > Example:
    >
    > My custom control containes
    > a table
    > one row
    > and three cells
    >
    > _____________________________
    > | Cell1 | Cell2 | Cell3 |
    > _____________________________
    >
    > I want to position my content in cell #2
    >
    > <HTML>
    > <BODY>
    > <CustomControl:myTable id="myTable1" runat="server">
    >
    > /**** this is what i want to position in cell #2 ****/
    > <tr>
    > <td>My text</td>
    > </tr>
    >
    > </CustomControl:myTable>
    > </BODY>
    > </HTML>
    >
    > Any suggestion as to how to accomplish this?
    >
    > Thanks,
    >
    > /ekegren
    >
    >
     
    Victor Garcia Aprea [MVP], Jul 20, 2003
    #2
    1. Advertising

  3. Hi Thomas,

    What I had in mind was a bit different. If I'm understanding you correctly
    you want your control to only allow childs controls to be added to an
    specific cell of a one-row table, is that correct? Currently in your code,
    you're deriving from Table, this would allow a page developer to actually do
    more than what I believe you want. He/she could add more rows, cells, etc;
    is this still okay?

    --
    Victor Garcia Aprea
    Microsoft MVP | ASP.NET
    Looking for insights on ASP.NET? Read my blog:
    http://obies.com/vga/blog.aspx
    To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
    and not by private mail.

    "Thomas Ekegren" <> wrote in message
    news:%...
    Hi Victor,
    I have now managed to but content into my cell as I wanted. The problem is
    that I can't get the controls that are nested inside my composite control to
    be displayed properly.
    So my question is how do I loop through the nested controls and get them
    displayed inside the table. Could you pinpoint me in the right direction and
    maybe explain why the code below does not work as intended?
    /Thomas
    here is my code:
    Custom Control:
    using System;
    using System.Reflection;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.ComponentModel;
    namespace cc
    {
    /// <summary>
    /// Summary description for WebCustomControl1.
    /// </summary>
    [DefaultProperty("Text"),
    ToolboxData("<{0}:CustomTable1 runat=server></{0}:CustomTable1>")]
    [ParseChildren(false)]
    public class CustomTable : System.Web.UI.HtmlControls.HtmlTable,
    INamingContainer
    {
    private string text;
    private HtmlTableCell c2 = new HtmlTableCell();
    [Bindable(true),
    Category("Appearance"),
    DefaultValue("")]
    public string Text
    {
    get
    {
    return text;
    }
    set
    {
    EnsureChildControls();
    c2.InnerHtml = value;
    }
    }
    protected override void CreateChildControls()
    {
    //string HtmlInsideCell2;
    HtmlTableRow tblRow = new HtmlTableRow();
    HtmlTableCell c1 = new HtmlTableCell();
    c1.InnerHtml = "Cell1";
    tblRow.Cells.Add(c1);
    tblRow.Cells.Add(c2);
    HtmlTableCell c3 = new HtmlTableCell();
    c3.InnerHtml= "Cell3";
    tblRow.Cells.Add(c3);
    this.Rows.Add(tblRow);
    //Controls.Add(tblRow);
    }
    protected override void AddParsedSubObject(Object obj)
    {
    //this is where the child objects are added to the cell#2
    HtmlTable tbl = new HtmlTable();
    if(obj.GetType() is HtmlTableRow)
    {
    HtmlTableRow r1 = (HtmlTableRow)obj;
    tbl.Rows.Add(r1);
    }
    c2.Controls.Add(tbl);
    }
    /// <summary>
    /// Render this control to the output parameter specified.
    /// </summary>
    /// <param name="output"> The HTML writer to write out to </param>
    protected override void Render(HtmlTextWriter output)
    {
    base.Render(output);
    }
    }
    }
    my aspx page:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>WebForm2</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema"
    content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="WebForm2" method="post" runat="server">
    <Custom:CustomTable id="ct1" runat="server">
    <TR>
    <TD>Hello World!</TD>
    </TR>
    </Custom:CustomTable>
    </form>
    </body>
    </HTML>

    "Thomas Ekegren" <> skrev i en meddelelse
    news:...
    > Hi,
    >
    > I want to create a custom control that implements the table htmlcontrol.

    In
    > my webform page I want to include that custom control and add content to a
    > cell in my custom table control. I don't want to do this dynamically in

    the
    > page_load event but in the HTML design view.
    >
    > Example:
    >
    > My custom control containes
    > a table
    > one row
    > and three cells
    >
    > _____________________________
    > | Cell1 | Cell2 | Cell3 |
    > _____________________________
    >
    > I want to position my content in cell #2
    >
    > <HTML>
    > <BODY>
    > <CustomControl:myTable id="myTable1" runat="server">
    >
    > /**** this is what i want to position in cell #2 ****/
    > <tr>
    > <td>My text</td>
    > </tr>
    >
    > </CustomControl:myTable>
    > </BODY>
    > </HTML>
    >
    > Any suggestion as to how to accomplish this?
    >
    > Thanks,
    >
    > /ekegren
    >
    >
     
    Victor Garcia Aprea [MVP], Jul 20, 2003
    #3
    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. =?Utf-8?B?V2ViTWF0cml4?=
    Replies:
    3
    Views:
    4,236
    Joe Fallon
    Dec 9, 2004
  2. Leeor Chernov
    Replies:
    2
    Views:
    793
    Leeor Chernov
    Oct 16, 2005
  3. I am Sam

    Building a custom table control

    I am Sam, May 18, 2005, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    116
    I am Sam
    May 18, 2005
  4. boarding_king

    Building a control adapter for a custom control

    boarding_king, Sep 20, 2006, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    141
    boarding_king
    Sep 20, 2006
  5. Ryan Taylor

    Building a custom server control - Property Builder

    Ryan Taylor, Feb 8, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    129
    Ryan Taylor
    Feb 8, 2005
Loading...

Share This Page