How to use the Table class in asp.net

Source: Internet
Author: User

You only need to drag a Table control to the front-end to give an ID, and all our operations can be performed in the background.

The code is as follows: Copy code
<Asp: table ID = "table" runat = "server" BorderColor = "#000000" BorderStyle = "Solid" BorderWidth = "0px" Caption = "Table title" CellPadding = "0" CellSpacing = "0" Font-Bold = "False" Font-Italic = "False" Font-Overline = "False" Font-Size = "30px" Font-Strikeout = "False" Font -Underline = "False" Width = "980px">
</Asp: Table>

The Table here is a control that allows you to change the rows and columns of a Table in the background.

The TableRow class is used to add a row. This class is equivalent to the tr label inside the table label in the HTML standard. To add a row, we can write as follows:

The code is as follows: Copy code
TableRow headUpTr = new TableRow ();
Table. Rows. Add (headUpTr)

In this way, a row is added to the table.

The method for adding a column in a row is the same as that in TableCell. Similarly, this class is equivalent to the td tag in HTML. Table, TableRow, and TableCell constitute a complete Table.

The code is as follows: Copy code
TableCell td = new TableCell ();
HeadUpTr. Cells. Add (td); TableCell

You can change the style of a table. When filtering the result set of data, you can customize the table based on different values in the loop output.

The most common TableCell attributes are:

ColumnSpan; used for columns

RowSpan; used for cross-row

Text; used to write strings

The Controls. Add (); method is used as the output control in the cell.

Attributes. Add (); this adds Attributes to cells and html tags, such as Style and Align.
Sometimes there may be many reuse cases, so I will write some operations as a method for convenient calls, such

The code is as follows: Copy code
Private void addtr (ref TableRow tr, string data, int colspan, int rowspan, string align, int width, string fontsize, int height, string style)
    {
TableCell td = new TableCell ();
Td. Width = width;
Td. Attributes. Add ("align", align );
Td. Attributes. Add ("style", fontsize );
Td. CssClass = style;
Td. ColumnSpan = colspan;
If (height! = 20)
        {
Td. Height = height;
        }
Td. RowSpan = rowspan;
Td. Text = data. ToString ();
Tr. Cells. Add (td );

} I think there should be a simpler and more efficient method, but at present I have limited capabilities, but I can only find this method. Although it is not very advanced, it may be helpful for beginners.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.