Why is it that a TableCell cannot have both Text and a Child Control?

Discussion in 'ASP .Net Building Controls' started by Robert Zurer, Aug 21, 2003.

  1. Robert Zurer

    Robert Zurer Guest

    Why are child controls and text mutually exclusive using ASP.NET?

    The code below gives me either one or the other?

    private void Page_Load(object sender, System.EventArgs e)
    {
    Control formMain = this.FindControl("Form1");
    Table table = new Table();
    TableRow row = new TableRow();
    TableCell cell = new TableCell();
    Button button = new Button();
    button.Text = "Button";
    formMain.Controls.Add(table);
    table.Rows.Add(row);
    row.Cells.Add(cell);


    cell.Text = "If I assign the text here it will not display";
    cell.Controls.Add(button);
    cell.Text = "If I assign the text here the button will not display";

    }

    The workaround to the above is to add a Literal.

    Is this an ASP.NET quirk? If i add the html below it works as expected.

    <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
    Inherits="MendesMatterManager.WebForm1" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>WebForm1</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <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="Form1" method="post" runat="server">
    <table border="0">
    <tr>
    <td>
    <asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
    This Text displays to the right of the button</td>
    </tr>
    </table>
    </form>
    </body>
    </HTML>

    TIA


    Robert Zurer
     
    Robert Zurer, Aug 21, 2003
    #1
    1. Advertising

  2. "Robert Zurer" <> wrote in message
    news:...
    > Why are child controls and text mutually exclusive using ASP.NET?
    >
    > The code below gives me either one or the other?
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > Control formMain = this.FindControl("Form1");
    > Table table = new Table();
    > TableRow row = new TableRow();
    > TableCell cell = new TableCell();
    > Button button = new Button();
    > button.Text = "Button";
    > formMain.Controls.Add(table);
    > table.Rows.Add(row);
    > row.Cells.Add(cell);
    >
    >
    > cell.Text = "If I assign the text here it will not display";
    > cell.Controls.Add(button);
    > cell.Text = "If I assign the text here the button will not display";


    Which should display first, the text or the button? And why do you think
    that's the way it should be?
    --
    John Saunders
    Internet Engineer
     
    John Saunders, Aug 22, 2003
    #2
    1. Advertising

  3. Robert Zurer

    Robert Zurer Guest

    "John Saunders" <> wrote in message
    news:...
    >
    > Which should display first, the text or the button? And why do you think
    > that's the way it should be?


    It really doesn't matter which displays first, neither combination works.

    It's just that it _can_ be done using straight html but it does not seem
    possible using WebControls created on the fly.

    It must be something in the way asp.net creates the html which renders
    controls in the browser.
     
    Robert Zurer, Aug 22, 2003
    #3
  4. "Robert Zurer" <> wrote in message
    news:...
    >
    > "John Saunders" <> wrote in message
    > news:...
    > >
    > > Which should display first, the text or the button? And why do you think
    > > that's the way it should be?

    >
    > It really doesn't matter which displays first, neither combination works.


    My point was: you can't do both because it's not possible to decide whether
    the text should come first or last.

    If you want both the text and the control, put the text in a LiteralControl
    or something.
    --
    John Saunders
    Internet Engineer
     
    John Saunders, Aug 22, 2003
    #4
  5. Robert Zurer

    Teemu Keiski Guest

    Yes, pretty much. Rendering logic of TableCell is in pseudo-code like this:

    -If cell has child controls render them and return
    -Otherwise write out the value in Text property.

    --
    Teemu Keiski
    MCP, Designer/Developer
    Mansoft tietotekniikka Oy
    http://www.mansoft.fi

    AspInsiders Member, www.aspinsiders.com
    ASP.NET Forums Moderator, www.asp.net
    AspAlliance Columnist, www.aspalliance.com


    "Robert Zurer" <> wrote in message
    news:...
    >
    > "John Saunders" <> wrote in message n
    > > My point was: you can't do both because it's not possible to decide

    > whether
    > > the text should come first or last.
    > >
    > > If you want both the text and the control, put the text in a

    > LiteralControl
    > > or something.
    > > --
    > >

    > I guess they did it by design. Thanks very much for your input.
    >
    > Robert Zurer
    >
    >
     
    Teemu Keiski, Aug 22, 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. Bob Voss
    Replies:
    1
    Views:
    507
    Joe Molloy
    Dec 18, 2003
  2. Brian K. Williams

    TableCell oTableCell = new TableCell(); noWrap?

    Brian K. Williams, Apr 30, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    2,595
    Brian K. Williams
    Apr 30, 2004
  3. Jeff Rodriguez
    Replies:
    23
    Views:
    1,203
    David Schwartz
    Dec 9, 2003
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,237
    Smokey Grindel
    Dec 2, 2006
  5. DC
    Replies:
    3
    Views:
    1,156
Loading...

Share This Page