Dynamic table rows don't appear on client page - Any idea why

R

Richard Lionheart

Hi,

I've got simple C#/WebForms VS.NET app that ostensibly populates a table
server-side, but no table is displayed client side. I tried a couple of
other dynamically generated pages with no luck there either. The code is
provided below. Any ideas?
--
Regards,
Richard

======== AddingRows.aspx =============
<%@ Page language="c#" Codebehind="AddingRows.aspx.cs"
AutoEventWireup="false" Inherits="TestAddingRowsToTable.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<!-- meta statements deleted -->
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:Label id="Label1" runat="server" Width="205px">Testing Dynamicly
Adding Rows</asp:Label>
<HR width="100%" SIZE="1">
<asp:Table id="MyTable" runat="server" Width="122px"></asp:Table>
</form>
</body>
</HTML>
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
for (int i=1; i<=2; i++)
{
TableRow r = new TableRow();
for (int j=1; j<=2; j++)
{
TableCell c = new TableCell();
c.Text = String.Format("Row {0}, Col (1}", i, j);
r.Cells.Add(c);
}
MyTable.Rows.Add(r);
}
}
</script>
 
W

Wiktor Zychla [C# MVP]

void Page_Load(Object sender, EventArgs e)
{
for (int i=1; i<=2; i++)
{
TableRow r = new TableRow();
for (int j=1; j<=2; j++)
{
TableCell c = new TableCell();
c.Text = String.Format("Row {0}, Col (1}", i, j);
r.Cells.Add(c);
}
MyTable.Rows.Add(r);
}
}

try this:

void Page_Load(Object sender, EventArgs e)
{
for (int i=1; i<=2; i++)
{
HtmlTableRow r = new HtmlTableRow();
for (int j=1; j<=2; j++)
{
HtmlTableCell c = new HtmlTableCell();
c.Controls.Add( new LiteralControl( String.Format("Row {0}, Col
{1}", i, j)));
r.Cells.Add(c);
}
MyTable.Rows.Add(r);
}
}


Regards,
Wiktor Zychla
 
R

Richard Lionheart

Hi Wiktor,

Thank you very much for responding to my plea for help. Subsequent to
posting my request, I received a response to a different request that turned
out to be related to this one. I turns out that I failed to change
AutoEventWireup="false" to "true". Once I did that, my table was rendered on
the client web page.

Nevertheless, I'm interested in learning alternative techniques, so I tried
switching from Table to Html table. However I ran into compilation problems:

1. MyTable.Rows.Add(r) was flagged because "CS1502: The best overloaded
method match for
'System.Web.UI.WebControls.TableRowCollection.Add(System.Web.UI.WebControls.TableRow)'
has some invalid arguments"

2. I tried switch MyTable's declaration from
"System.Web.UI.WebControls.Table " to
"System.Web.UI.HtmlControls.HtmlTable", but that led to an error in the
statement in the statement adding MyTable to the form: "The base class
includes the field 'MyTable', but its type
(System.Web.UI.HtmlControls.HtmlTable) is not compatible with the type of
control (System.Web.UI.WebControls.Table)"

I also noted that the HTML controls were not available in my Toolbox when I
was building this WebForm application. I wonder is an omen that we won't be
able to use any HTML controls in this context. Or is there a way I can make
them available?

Incidentally, I just found a C# example at
http://msdn.microsoft.com/library/d...ystemwebuihtmlcontrolshtmltableclasstopic.asp,
which I'll study.

Again, thanks for your help.

Regards,

Richard
 
R

Richard Lionheart

Hi Wiktor,

The C# portion of the example at
http://msdn.microsoft.com/library/d...ystemwebuihtmlcontrolshtmltableclasstopic.asp
worked beautifully with HtmlTable:

I:
- Created a new C# Web Forms app in VS.NET
- Copied script from the example to the new project's HEAD
- Copied form content the from example to the new project's form

Works perfectly!! And I owe discovery of this example to you, because I
found it be searching for HtmlTable. Furthermore, it's a much richer
example than my puny one.

Regards,
Richard
 

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

Top