How to dynamically add TableRows in asp.net?

J

Jim Bancroft

Hi everyone,

I was wondering how best to dynamically add rows to an html table in
asp.net. I've been reading up on PlaceHolder controls in .Net, but every
time I add a new control to the collection it scrolls horizontally. I need
a vertical column.

Here's the ASP code I'm trying to port over. I've simplified it a little
for demonstration. Basically just looping through a recordset and adding
rows to a table, one for each record:


<table border="1">

<%if not (adoRecSet.bof and adoRecSet.eof) then
counter = 0
do while not adoRecSet.eof
%>

<tr>
<td>Display Name: <input type="text" width=15
name="<%="txtDisplayName" & counter%>"
value="<%=adoRecSet("tDisplayName")%>"></td>
</tr>

<%
adoRecSet.movenext
counter = counter + 1
loop
End if%>

</table>


I'd like to accomplish the same thing in .Net but can't figure how to do it.
One thought: is it possible to add a table to my PlaceHolder control and
then add rows from there? Thanks for your help.

-Jim
 
D

Daniel M. Hendricks

Example:

<%@ Page Language="C#" %>

<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
AddRow("Name", "Daniel Hendricks");
AddRow("Phone", "555-1212");
AddRow("Location", "Milwaukee, Wisconsin");
}

private void AddRow(string lbl, string val) {
TableCell cell1 = new TableCell();
TableCell cell2 = new TableCell();
cell1.Text = lbl + ": &nbsp;";
//cell1.Width = 130;
cell1.HorizontalAlign = HorizontalAlign.Right;
cell2.Text = val;

TableRow row = new TableRow();
row.Cells.Add(cell1);
row.Cells.Add(cell2);
}
</script>

<html>
<body>

<asp:table runat="server" id="tblDetail" />

</body>
</html>


Jim said:
Hi everyone,

I was wondering how best to dynamically add rows to an html table in
asp.net. I've been reading up on PlaceHolder controls in .Net, but every
time I add a new control to the collection it scrolls horizontally. I need
a vertical column.

Daniel M. Hendricks
http://www.danhendricks.com
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top