• Table控件使用示例


    代码
    <%@ 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 表并以直接方式指定其属性。可以用给定的静态内容在设计时生成表,但 Table Web 服务器控件的威力通常在用动态内容以编程方式生成表时才会体现出来。

    值得注意的是,以编程方式对表行或单元格所做的任何添加或修改不在向服务器的发送间保持。这是因为表行和单元格本身就是控件,而不是Table 控件的属性。要保持对表所做的任何更改,必须在每次回送后重新构造行和单元格。实际上,如果需要进行实质性的修改,建议使用 DataList、DataGrid 或 GridView 控件,而不是Table 控件。因此,该Table 类主要由控件开发人员使用。

    将在每个单元格中显示静态文本和 Hyperlink 控件。Hyperlink 控件定位到一个模拟的 URL,传递一个模拟产品 ID。由于该示例混合了静态文本和控件,因此静态文本Literal 对象的形式实现,像Hyperlink 控件一样添加到单元格的Controls 集合中。

    代码
    Protected void Button1_Click (object sender, System.EventArgs e)
    {
       
    // 总行数.
       int rowCnt;
       
    // 当前行号.
       int rowCtr;
       
    // 每行列数.
       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);
          }
       }
    }
  • 相关阅读:
    密码等级
    ie兼容透明
    分割线
    支付宝银行判断接口
    date只能选择今天之后的时间js
    离开页面之前提示,关闭,刷新等
    使用 Linux 系统的常用命令
    C#窗体简单增删改查
    1
    二维数组
  • 原文地址:https://www.cnblogs.com/Fskjb/p/1665129.html
Copyright © 2020-2023  润新知