Click to See Complete Forum and Search --> : HTML Writing with ASP.NET


Taurian110
October 13th, 2005, 02:15 PM
In classic ASP we would have an HTML page and an ASP page.

In HTML page we would have a place holder something like this:


<form>
<table>
<!-- Values -->
</table>
</form>


In ASP page we would replace the "<!-- Values -->" to the rest of the table information, something like this:


stringTable = "<tr><td>" & tableRS("FieldName") & "</td></tr>"

strOutPutHTML = Replace(strOutPutHTML , "<!-- Values -->", stringTable)
response.write(strOutPutHTML)


How can we acomplish this in ASP.NET code behind using C#?

cmiskow
October 13th, 2005, 05:05 PM
In ASP.NET, everything is handled with objects in the code-behind. Your example would be easy to do with a DataGrid, like so:

in the aspx HTML section:

<asp:DataGrid ID="dgResults" runat="server">
<Columns>
<asp:BoundColumn DataField="FieldName"></asp:BoundColumn>
</Columns>
</asp:DataGrid>


in the code-behind page (in the Page Load event for example):

// omitted: code for obtaining tableRS
dgResults.DataSource = tableRS;
dgResults.DataBind();


There are many options for things like this. If you want more control over your table, you can use a Table webcontrol and in the code you add TableRow objects to it and TableCell objects to the rows. For example:

private void loadTable()
{
for (int i = 0; i < 10; i++)
{
TableRow row = new TableRow();
for (int j = 0; j < 5; j++)
{
TableCell cell = new TableCell();
cell.Text = i.ToString() + ", " + j.ToString();
row.Cells.Add(cell);
}
myTable.Rows.Add(row);
}
}

would give you a 5x10 table with the row number and the column number in each cell.

Taurian110
October 14th, 2005, 09:35 AM
Thanks. I appreciate the help.