Generate Table programmatically in ASP.NET using VB.NET

In this article you learn that how can you manipulate table programmatically with Table control.
  • 7365
Download Files:
 

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 Table Row and Table Cell 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 data source. 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.

Categories

More Articles

© 2013 dotNetheaven. All rights reserved.