Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

FREE 12 month online training for ASP.NET & MS Expression Studio and a Free copy of MS Expression Web with Windows Server Purchase
Creating a Table Control Using ASP.Net and Visual Basic.Net

Author: Faraz

In the article I will show you the technique of a creating table control in code. I will also discuss some of the formatting tips.

You may find that you need to write out to visitors’ browsers a basic HTML table. But that table cannot be predefined. It may depend on some visitors input or it may depend on the values in a database table. This technique shows you how to create a Table control in code and display that control on the rendered page.

 

The web form defined for this technique creates a Table control when the page is first loaded. The table displays the coordinates of each cell through the rendered HTML table. For this you only have to define a Place Holder control on the web form.

 

Now add the following code in the code behind:

 

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'Put user code to initialize the page here

Dim addTable As Table = New Table

addTable.BorderStyle = BorderStyle.Solid

addTable.BorderWidth = Unit.Pixel(1)

addTable.BorderColor = Color.Chocolate

addTable.CellPadding = 1

addTable.CellSpacing = 0

addTable.GridLines = GridLines.Both

Dim addRow As Integer

Dim addCol As Integer

For addRow = 1 To 3

    Dim newRow = New TableRow

        For addCol = 1 To 4

           Dim newCell = New TableCell

             newCell.Text = addRow & "," & addCol

             newRow.Cells.add(newCell)

        Next

    addTable.Rows.add(newRow)

Next

Me.ph1.Controls.Add(addTable)

End Sub

First, you defined a Table control:

 

Dim addTable As Table = New Table

 

Then you select the format in which you want the table to be displayed:

 

addTable.BorderStyle = BorderStyle.Solid

addTable.BorderWidth = Unit.Pixel(1)

addTable.BorderColor = Color.Chocolate

addTable.CellPadding = 1

addTable.CellSpacing = 0

addTable.GridLines = GridLines.Both

 

Define variables that will be used in loop to determine the number of rows and columns:

 

Dim addRow As Integer

Dim addCol As Integer

Now using the loops add the Rows and Columns on the Form:

 

 For addRow = 1 To 3

    Dim newRow = New TableRow

        For addCol = 1 To 4

           Dim newCell = New TableCell

             newCell.Text = addRow & "," & addCol

             newRow.Cells.add(newCell)

        Next

    addTable.Rows.add(newRow)

Next

Here the outer loop is used for Rows and inner loop is used for columns. Finally the table is added to the page using Place Holder control:

 

Me.ph1.Controls.Add(addTable)

 

Summary:

  • Create a web form add the place holder control set its id property.
  • Add the above mention code in the code behind.
  • Run the application and use it in your future applications.
Article Comments
please give me a simple asp.net site project code...

Posted on 9/15/2010 2:47:32 PM by ekta namdeo

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< RequiredFieldValidator Control in ASP.Net

Disclaimer - Privacy
© 2002-2012 DevASP.net