Nannette Thacker ShiningStar.net

ASP.net Technologies.

HTML Tables to ASP.net Table Controls

By Nannette Thacker

If you're new to ASP.net, but familiar with ASP Classic or HTML, you might wonder about the ASP.net table control.

Dragging an HTML table from your toolbox...



... will generate these table commands:

<table>
    <tr>
        <td>
        </td>
        <td>
        </td>
        <td>
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td>
        </td>
        <td>
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td>
        </td>
        <td>
        </td>
    </tr>
</table>
But since we want to stick with the new ASP.net controls, we'll instead drag our Standard Table control onto our form.



But all it does is generate this code:

<asp:Table ID="Table1" runat="server">
</asp:Table>
The above code simply looks like this on the screen:



What in the world do you do with that?

At this point, right click the Table in the Design view and select Properties.



The Properties window will display with our Table1 properties.



To assign Rows, go to the bottom and select the Rows property. A little button will appear on the right [...], select it.



This will open the TableRow Collection Editor with properties for your row, <TR>. Select "Add" to add rows to your table.



To add Cells or "<TD>"s, go to the bottom of the TableRow Properties and select the "Cells" Property. A little button will appear [...]; select it. This opens the TableCell Collection Editor. Select "Add" to add columns to your table.



Select OK to close each window. Now look at your code.

We have inserted a few words within our TableCell properties. Now our finished code looks like this:

<asp:Table ID="Table2" runat="server">
    <asp:TableRow ID="TableRow1" runat="server">
        <asp:TableCell ID="TableCell1" runat="server">
        This is Cell 1
        </asp:TableCell>
        <asp:TableCell ID="TableCell2" runat="server">
        This is Cell 2
        </asp:TableCell>
    </asp:TableRow>
</asp:Table>

On screen, it looks like this:



But where are the other common values you are used to using within tables, rows and cells?

Let's play around with our Properties. You may set the Table's Appearance. Notice here, I set the BackColor to White, the BorderColor to Red and the BorderWidth to 1px. I also set CellPadding and CellSpacing to 2 and assigned a CssClass. (If you set the class, it can handle the majority of these properties for you, but that's another story.) The Font Forecolor is set to Navy and Gridlines to Both. Play around with the Properties and you'll likely find other values you may need.



In our Layout Property, I set the HorizontalAlign to "Center."



Now I want to skip over to the TableCell Collection Editor and set the TableCell alignment to "Right" for the first cell and Wrap to False.



Our finished table now looks like this:

This is Cell 1 This is Cell 2

And the generated code looks like this:
<asp:Table ID="Table1" runat="server" BackColor="White" 
    BorderColor="Red" BorderWidth="1px" 
    CellPadding="2" CellSpacing="2" 
    CssClass="tablecell" ForeColor="Navy" 
    GridLines="Both" HorizontalAlign="Center">
        <asp:TableRow runat="server">
            <asp:TableCell runat="server" 
                HorizontalAlign="Right" Wrap="False">
                This is Cell 1
                </asp:TableCell>
            <asp:TableCell runat="server" 
                HorizontalAlign="Left" 
                VerticalAlign="Middle">
                This is Cell 2
                </asp:TableCell>
        </asp:TableRow>
</asp:Table>

From the above, you'll see the "Align" in an HTML Table is replaced by "HorizontalAlign." "Valign" is replaced with "VerticalAlign." "Nowrap" is replaced with Wrap="False."

Play around with the ASP.net Table control and you'll soon find all the missing HTML pieces.

If you View your Table in a web browser and View the Source Code, it has been generated client side to look like this. (I have added line breaks so it will fit on the screen.)

<table id="Table1" class="tablecell" cellspacing="2" 
  cellpadding="2" align="Center" rules="all" border="1" 
  style="color:Navy;background-color:White;border-color:Red;
	border-width:1px;border-style:solid;">
	<tr>
	  <td align="right" style="white-space:nowrap;">
           This is Cell 1
          </td>
          <td align="left" valign="middle">
           This is Cell 2
          </td>
	</tr>
</table>
Nannette
I want to be a Paladin when I grow up!

Comments

Paolo said:

Geat job!!!!!!!!

# February 7, 2008 6:32 AM

Shannon said:

Hmm, I'm not sure..

I never saw the point in using the ASP.NET Table

# February 18, 2008 8:30 AM

nannette said:

Shannon,

I do admit, when HTML flies out of my fingertips, it's hard to motivate myself to use the control.

msdn2.microsoft.com/.../9f65szta(VS.71).aspx

In reading the docs, I think the main benefit is that you can create the control programmatically and add dynamic content.

Of course, ASP Classic developers have been doing that for years anyway by using the HTML table and building a string.

In this article:

msdn2.microsoft.com/.../system.web.ui.webcontrols.table.aspx

It says:

The Table class is primarily used by control developers.

# February 18, 2008 11:21 AM

ŞENOL said:

good

help pls osc3.template-help.com/osc_19050

What's New Here? code

# April 26, 2008 8:48 AM

shiva said:

How can we add line break to Asp.Net Table Control

# July 14, 2008 12:06 PM

Eric said:

Nannette,

Thank you for your interesting article.

Could you please let me know where can I find complete documentation on the asp table control?

Thank you for your trouble.

Eric

# September 8, 2008 5:22 PM

rajasekhar said:

this is the waster article......ur cheating....

i need html table properties..but u present the asp table control......i don't need to burden my server code....

# September 10, 2008 3:01 AM

nannette said:

Eric,

This MSDN article should answer those questions:

msdn.microsoft.com/.../system.web.ui.webcontrols.table.aspx

Nannette

# September 12, 2008 11:41 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)