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

R

Robert Zurer

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
 
J

John Saunders

Robert Zurer said:
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?
 
R

Robert Zurer

John Saunders said:
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.
 
J

John Saunders

Robert Zurer said:
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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top