Getting dynamic controls/tables to render on page

J

jsever05

I have looked around the web for a solution to this, but I can't find
it. It's probably something simple I have overlooked.

I am trying to build a dynamic table on the server-side and render its
contents to the .aspx page. I placed the following code on the .aspx
page:


<asp:Table id="TestTable" runat="server"></asp:Table>


Then in my page class, I added the following member variable:


System.Web.UI.HtmlControls.HtmlTable TestTable;


Finally, in the Page_Init() function I added the following code:


TestTable = new HtmlTable();

HtmlTableRow theRow = new HtmlTableRow();

TestTable.Rows.Add(theRow);

HtmlTableCell theCell = new HtmlTableCell();

theRow.Cells.Add(theCell);

HyperLink theLink = new HyperLink();

theCell.Controls.Add(theLink);

theLink.Target = "StatusOrders.aspx";
theLink.ID ="link1";
theLink.Text = "Test Text aaaaa";


When I run the code, the only thing that renders as HTML is:

<table id="TestTable" border="0"></table>

I get nothing for the row, cell, or link that I added. I tried putting
this code in the Page_Load() event, the Page_Init() event, and a
constructor function that I created for the page object, and it doesn't
change. Can someone help me to render this properly?

Thanks!
 
S

sfarriss

Is there a reason you have this line -> TestTable = new HtmlTable();

You have already declared it as new by <asp:Table id="TestTable"
runat="server"></asp:Table> on the page itself.

If you want a dynamic table just use:
HtmlTable ht = new HtmlTable();
this.controls.add(ht);

Also, if you leave it on the page you will want to declare it as
protected System.Web.UI.HtmlControls.HtmlTable TestTable;

HTH
 
J

JS

The main reason I added the new HtmlTable() line is because if I didn't
it gives me a run time error saying the object did not exist. However,
I just tried it again by converting everything from HtmlTable, HtmlRow,
etc. to just Table, Row, Cell, etc. and I didn't get that error.

However, I still can't get the dynamic rows, cells, and cell contents
to actually render as HTML. Maybe I am asking the wrong question. Here
is the right question:

Can someone give me a simple step-by-step example of how to create a
dynamic table with a variable number of rows, cells, etc., position it
on the page where I want it, and have it render and display? It should
only take a few steps, I believe...
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top