<html>
<head>
<title>Generating Table Rows and Cells Dynamically</title>
<link rel = "stylesheet" href = "/shared/netdemos.css">
<script language = "C#" runat = "server">
void Page_Load ( object src, EventArgs e ) {
if ( !IsPostBack ) {
// generate select options
for ( int i = 1; i <= 5; i++ ) {
rowsSelect.Items.Add ( i.ToString ( ) );
cellsSelect.Items.Add ( i.ToString ( ) );
}
rowsSelect.SelectedIndex = 1;
cellsSelect.SelectedIndex = 2;
}
// generate rows and cells
int numrows = int.Parse ( rowsSelect.SelectedItem.Value );
int numcells = int.Parse ( cellsSelect.SelectedItem.Value );
for ( int j = 0; j < numrows; j++ ) {
TableRow r = new TableRow ( );
for ( int i = 0; i < numcells; i++ ) {
TableCell c = new TableCell ( );
c.Controls.Add ( new LiteralControl ( "row " + j.ToString ( ) + ", cell " + i.ToString ( ) ) );
r.Cells.Add ( c );
}
myTable.Rows.Add ( r );
}
}
</script>
</head>
<body>
<!-- #include virtual="~/shared/top.inc -->
<div class="header"><h2>Generating Table Rows and Cells Dynamically</h2></div>
<!-- #include virtual="~/shared/viewsrc_top.inc" -->
<hr size=1 width=92%>
<div align="center">
<form runat="server">
<p><asp:table id="myTable" cellpadding=5 cellspacing=0 gridlines="Both" runat="server" /></p>
<p>
Table rows <asp:dropdownlist id="rowsSelect" runat="server" />
Table cells <asp:dropdownlist id="cellsSelect" runat="server" />
<p><asp:button text="Generate Table" runat="server" />
</form>
</div>
<hr size=1 width=92%>
<!-- #include virtual="~/shared/viewsrc.inc" -->
</body>
</html>