all groups > asp.net building controls > august 2003 >
You're in the

asp.net building controls

group:

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


Why is it that a TableCell cannot have both Text and a Child Control? Robert Zurer
8/21/2003 6:03:08 PM
asp.net building controls: 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


Re: Why is it that a TableCell cannot have both Text and a Child Control? John Saunders
8/21/2003 8:04:03 PM
[quoted text, click to view]

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@surfcontrol.com

Re: Why is it that a TableCell cannot have both Text and a Child Control? Robert Zurer
8/21/2003 8:25:24 PM

[quoted text, click to view]

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.

Re: Why is it that a TableCell cannot have both Text and a Child Control? John Saunders
8/21/2003 8:30:21 PM
[quoted text, click to view]

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@surfcontrol.com

AddThis Social Bookmark Button