![]() <%@ Page language="C#" AutoEventWireup="true" %> <%@ Import Namespace="System.Drawing" %> <html> <head> <script runat="server"> private void Page_Load(object sender, System.EventArgs e) { // Create a TableItemStyle object that can be // set as the default style for all cells // in the table. TableItemStyle tableStyle = new TableItemStyle(); tableStyle.HorizontalAlign = HorizontalAlign.Center; tableStyle.VerticalAlign = VerticalAlign.Middle; tableStyle.Width = Unit.Pixel(100); // Create more rows for the table. for (int i = 2; i < 10; i++) { TableRow tempRow = new TableRow(); for (int j = 0; j < 3; j++) { TableCell tempCell = new TableCell(); tempCell.Text = "(" + i + "," + j + ")"; tempRow.Cells.Add(tempCell); } Table1.Rows.Add(tempRow); } // Apply the TableItemStyle to all rows in the table. foreach (TableRow r in Table1.Rows) foreach (TableCell c in r.Cells) c.ApplyStyle(tableStyle); // Create a header for the table. TableHeaderCell header = new TableHeaderCell(); header.RowSpan = 1; header.ColumnSpan = 3; header.Text = "Table of (x,y) Values"; header.Font.Bold = true; header.BackColor = Color.Gray; header.HorizontalAlign = HorizontalAlign.Center; header.VerticalAlign = VerticalAlign.Middle; // Add the header to a new row. TableRow headerRow = new TableRow(); headerRow.Cells.Add(header); // Add the header row to the table. Table1.Rows.AddAt(0, headerRow); } </script> </head> <body> <form runat="server"> <h1>TableCell Example</h1> <asp:table id="Table1" runat="server" CellPadding="3" CellSpacing="3"> <asp:TableRow> <asp:TableCell Text="(0,0)"></asp:TableCell> <asp:TableCell Text="(0,1)"></asp:TableCell> <asp:TableCell Text="(0,2)"></asp:TableCell> </asp:TableRow> <asp:TableRow> <asp:TableCell Text="(1,0)"></asp:TableCell> <asp:TableCell Text="(1,1)"></asp:TableCell> <asp:TableCell Text="(1,2)"></asp:TableCell> </asp:TableRow> </asp:table> </form> </body> </html>
Table控件允許您生成 HTML 表并以直接方式指定其屬性。可以用給定的靜態(tài)內(nèi)容在設(shè)計時生成表,但 Table Web 服務(wù)器控件的威力通常在用動態(tài)內(nèi)容以編程方式生成表時才會體現(xiàn)出來。 值得注意的是,以編程方式對表行或單元格所做的任何添加或修改不在向服務(wù)器的發(fā)送間保持。這是因為表行和單元格本身就是控件,而不是Table 控件的屬性。要保持對表所做的任何更改,必須在每次回送后重新構(gòu)造行和單元格。實際上,如果需要進行實質(zhì)性的修改,建議使用 DataList、DataGrid 或 GridView 控件,而不是Table 控件。因此,該Table 類主要由控件開發(fā)人員使用。
將在每個單元格中顯示靜態(tài)文本和 Hyperlink 控件。Hyperlink 控件定位到一個模擬的 URL,傳遞一個模擬產(chǎn)品 ID。由于該示例混合了靜態(tài)文本和控件,因此靜態(tài)文本Literal 對象的形式實現(xiàn),像Hyperlink 控件一樣添加到單元格的Controls 集合中。
![]() Protected void Button1_Click (object sender, System.EventArgs e) { // 總行數(shù). int rowCnt; // 當前行號. int rowCtr; // 每行列數(shù). int cellCtr; // 當前列號. int cellCnt; rowCnt = int.Parse(TextBox1.Text); cellCnt = int.Parse(TextBox2.Text); for(rowCtr=1; rowCtr <= rowCnt; rowCtr++) { // Create a new row and add it to the table. TableRow tRow = new TableRow(); Table1.Rows.Add(tRow); for (cellCtr = 1; cellCtr <= cellCnt; cellCtr++) { // Create a new cell and add it to the row. TableCell tCell = new TableCell(); tRow.Cells.Add(tCell); // Mock up a product ID. string prodID = rowCtr + "_" + cellCtr; // Add a literal text as control. tCell.Controls.Add(new LiteralControl("Buy: ")); // Create a Hyperlink Web server control and add it to the cell. System.Web.UI.WebControls.HyperLink h = new HyperLink(); h.Text = rowCtr + ":" + cellCtr; h.NavigateUrl = "http://www.microsoft.com/net"; tCell.Controls.Add(h); } } }
|
|