ARTICLE

Using Table Control in ASP.NET

Posted by Sapna Articles | ASP.NET using VB.NET January 17, 2011
The ASP.NET Table Web server control allows you to build an table in conjunction with the TableCell control and the TableRow control.
 
Reader Level:

The Table Web server control allows you to build an table in conjunction with the TableCell control and the TableRow control. The following sample illustrates using the Table control in ASP.NET.   

Design View

asptable2.gif

As you can see here we can build up a table programmatically by adding TableRows to the Rows collection of the table, and TableCells to the Cells collection of the row.  You can add content to a table cell programmatically by adding controls to the Controls collection of the cell.

Properties of Table control

asptable1.gif

You can set the properties of the table like Backcolor, Borderstyle, Fontsize, Forecolor etc., with the help of Property Window in Design View.

Code

<html>
<head>
  <title>Example</title>
  <script language="VB" runat="server">
     Sub Page_Load(sender As Object, e As EventArgs)         
        Dim numrows As Integer
        Dim numcells As Integer
        Dim i As Integer
        Dim j As Integer
        Dim r As TableRow
        Dim c As TableCell
        ' Generate rows and cells
        numrows = CInt(DropDown1.SelectedItem.Value)
        numcells = CInt(DropDown2.SelectedItem.Value)          
        For j = 0 To numrows-1
             r = new TableRow()               
             For i = 0  To numcells-1
                 c.Controls.Add(new LiteralControl("row " & j & ", cell " & i))
                 r.Cells.Add(c)
             Next i
                 Table1.Rows.Add(r)
        Next j
     End Sub
 </script>
</head>
<body>
  <h3><font face="Verdana" style="font-family: Verdana; font-size: small;">ASP.NET Table 
      Control</font></h3>
  <form id="Form1" runat="server">
     <asp:Table id="Table1" Font-Names="Verdana" Font-Size="10pt" CellPadding=5 
          CellSpacing=0 BorderColor="black" BorderWidth="1px" Gridlines="Both" 
          runat="server" BackColor="White" BorderStyle="Solid" ForeColor="#990033" 
          Font-Bold="True"/>
     <p style="font-family: Verdana; font-size: small;">
          Insert rows in Table:
     <asp:DropDownList id="DropDown1" runat="server">
          <asp:ListItem Value="1">1</asp:ListItem>
          <asp:ListItem Value="2">2</asp:ListItem>
          <asp:ListItem Value="3">3</asp:ListItem>
          <asp:ListItem Value="4">4</asp:ListItem>
          <asp:ListItem Value="5">5</asp:ListItem>
          <asp:ListItem Value="6">6</asp:ListItem>
          <asp:ListItem Value="7">7</asp:ListItem>
          <asp:ListItem Value="8">8</asp:ListItem>
          <asp:ListItem Value="9">9</asp:ListItem>
          <asp:ListItem Value="10">10</asp:ListItem>
     </asp:DropDownList>
          <br />
     <br>
         Insert cells in Table:
     <asp:DropDownList id="DropDown2" runat="server">
          <asp:ListItem Value="1">1</asp:ListItem>
          <asp:ListItem Value="2">2</asp:ListItem>
          <asp:ListItem Value="3">3</asp:ListItem>
          <asp:ListItem Value="4">4</asp:ListItem
          <asp:ListItem Value="5">5</asp:ListItem>      
          <asp:ListItem Value="6">6</asp:ListItem
>
          <asp:ListItem Value="7">7</asp:ListItem>
          <asp:ListItem Value="8">8</asp:ListItem>
          <asp:ListItem Value="9">9</asp:ListItem>
          <asp:ListItem Value="10">10</asp:ListItem>
      </asp:DropDownList> 
      <p>
      <asp:button ID="Button1" Text="Create Table" runat="server" Font-Bold="True" 
                Font-Names="Verdana" Font-Size="Small"/>
  </form>
</body>
</html>

Table Output 

asptable.gif

Now if you want to change the background of the table, choose an image and give Url of that image to the table property BackImgUrl in the Property Window. The table looks like as below:

Table With Backgroun Image

asptable3.gif

I hope that I am able to convey the concept of ASP.NET Table control.

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Diagram
Become a Sponsor