<%@ 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 表并以直接方式指定其屬性??梢杂媒o定的靜態(tài)內(nèi)容在設(shè)計(jì)時(shí)生成表,但 Table Web 服務(wù)器控件的威力通常在用動(dòng)態(tài)內(nèi)容以編程方式生成表時(shí)才會(huì)體現(xiàn)出來。
值得注意的是,以編程方式對(duì)表行或單元格所做的任何添加或修改不在向服務(wù)器的發(fā)送間保持。這是因?yàn)楸硇泻蛦卧癖旧砭褪强丶?,而不是Table 控件的屬性。要保持對(duì)表所做的任何更改,必須在每次回送后重新構(gòu)造行和單元格。實(shí)際上,如果需要進(jìn)行實(shí)質(zhì)性的修改,建議使用 DataList、DataGrid 或 GridView 控件,而不是Table 控件。因此,該Table 類主要由控件開發(fā)人員使用。
將在每個(gè)單元格中顯示靜態(tài)文本和 Hyperlink 控件。Hyperlink 控件定位到一個(gè)模擬的 URL,傳遞一個(gè)模擬產(chǎn)品 ID。由于該示例混合了靜態(tài)文本和控件,因此靜態(tài)文本Literal 對(duì)象的形式實(shí)現(xiàn),像Hyperlink 控件一樣添加到單元格的Controls 集合中。
代碼
Protected void Button1_Click (object sender, System.EventArgs e)
{
// 總行數(shù).
int rowCnt;
// 當(dāng)前行號(hào).
int rowCtr;
// 每行列數(shù).
int cellCtr;
// 當(dāng)前列號(hào).
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);
}
}
}
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。