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";

    cell.Text = "If I assign the text here it will not display";
    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" >
    <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"
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <table border="0">
    <asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
    This Text displays to the right of the button</td>


    Robert Zurer
    Robert Zurer, Aug 21, 2003
    1. Advertisements

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

  3. Robert Zurer

    Robert Zurer Guest

    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
  4. 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, Aug 22, 2003
  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

    AspInsiders Member, www.aspinsiders.com
    ASP.NET Forums Moderator, www.asp.net
    AspAlliance Columnist, www.aspalliance.com
    Teemu Keiski, Aug 22, 2003
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.