ARTICLE

How to manipulate Table programmatically in ASP.NET using VB.NET

Posted by Shalini Juneja Articles | ASP.NET using VB.NET May 02, 2011
In this article you learn that how can you manipulate table programmatically with Table control.
Download Files:
 
Reader Level:


Introduction

The Table web server control allows us to build an HTML TABLE and helps us organize data in tabular form. Tables can be created at design time or run time. To create a table we also need the TableRow and TableCell web controls. If you create a table at design time you often fill its content with static data but if you create it at runtime that you can fill it with dynamic content like binding it to a datasource. The class hierarchy for table control is as follows
 

  • Object
  • Control 
  • WebControl 
  • Table

Properties of Table Control
 
  • CellPadding:- Gets/Sets the distance between the border and the content of the table cell. 
  • CellSpacing:- Gets/Sets the distance between table cells. 
  • GridLines:- Gets/Sets the GridLine property of the Table class.
  • Rows:- Gets/Sets the collection of rows within the table.


Here we are discussing an example in which we are manipulating table Programmatically with table control.

 

Getting Started
  • Simply create a new ASP.NET web application. 
  • Drag the two DropDownList, Button, Table control on your page. The page will look like below.

    table1.gif
     
  • Then attach the below code.

    <%@ Page Language="VB" %>
    <script runat="server">
    Sub
    Button1_Click(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
    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 =
    new TableCell()
    c.Controls.Add(
    new LiteralControl("row " & j & ", cell " & i))
    r.Cells.Add(c)
    Next
    Table1.Rows.Add(r)
    Next j
    End Sub
    </
    script>
    <
    html>
    <
    head>
    <
    title>vbdotnetheaven.Com Article</title>
    </
    head>
    <
    body>
    <
    form id="Form1" runat="server">
    Table rows:
    <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:DropDownList>
    Table cells:
    <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:DropDownList>
    <
    asp:button id="Button1" onclick="Button1_Click" runat="server" Text="Generate Table"></asp:button>
    <
    hr>
    <
    asp:Table id="Table1" runat="server" Gridlines="Both" BorderWidth="1" BorderColor="black" CellSpacing="0" CellPadding="5" Font
    Size
    ="8pt" Font-Names="Verdana"></asp:Table>
    </
    form>
    </
    body>
    </
    html>
     
  • Now run your application.



Output:-

table2.gif

table3.gif

table4.gif

table5.gif

table6.gif

Summary


In this article you learned that how to manipulate table programmatically.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
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.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor