HtmlTableRowCollection.Insert(Int32, HtmlTableRow) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將 HtmlTableRow 物件加入至集合中指定的位置。
public:
void Insert(int index, System::Web::UI::HtmlControls::HtmlTableRow ^ row);
public void Insert (int index, System.Web.UI.HtmlControls.HtmlTableRow row);
member this.Insert : int * System.Web.UI.HtmlControls.HtmlTableRow -> unit
Public Sub Insert (index As Integer, row As HtmlTableRow)
參數
- index
- Int32
HtmlTableRowCollection 中要加入 HtmlTableRow 的位置。
- row
- HtmlTableRow
要加入至 HtmlTableRow 的 HtmlTableRowCollection。
範例
下列程式碼範例示範如何使用 Insert 方法,將 物件 HtmlTableRowCollection 所 HtmlTableRow 表示之資料表的資料列插入集合中。
<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
// Get the number of rows and columns selected by the user.
int numrows = Convert.ToInt32(Select1.Value);
int numcells = Convert.ToInt32(Select2.Value);
// Iterate through the rows.
for (int j = 0; j < numrows; j++)
{
// Create a new row and add it to the Rows collection.
HtmlTableRow row = new HtmlTableRow();
// Provide a different background color for alternating rows.
if (j % 2 == 1)
row.BgColor = "Gainsboro";
// Iterate through the cells of a row.
for (int i = 0; i < numcells; i++)
{
// Create a new cell and add it to the Cells collection.
HtmlTableCell cell = new HtmlTableCell();
cell.Controls.Add(new LiteralControl("row " +
j.ToString() +
", cell " +
i.ToString()));
row.Cells.Insert(i, cell);
}
Table1.Rows.Insert(j, row);
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>HtmlTableRowCollection Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>HtmlTableRowCollection Example</h3>
<table id="Table1"
runat="server"
cellspacing="0"
style="padding:5; border-width:1; border-color:Black"/>
<hr />
Select the number of rows and columns to create: <br /><br />
Table rows:
<select id="Select1"
runat="server">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
Table cells:
<select id="Select2"
runat="server">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<br /><br />
<input type="submit"
value="Generate Table"
runat="server"/>
</form>
</body>
</html>
<%@ Page Language="VB" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim i As Integer
Dim j As Integer
Dim row As HtmlTableRow
Dim cell As HtmlTableCell
' Get the number of rows and columns selected by the user.
Dim numrows As Integer = CInt(Select1.Value)
Dim numcells As Integer = CInt(Select2.Value)
' Iterate through the rows.
For j = 0 To numrows - 1
' Create a new row and add it to the Rows collection.
row = New HtmlTableRow()
' Provide a different background color for alternating rows.
If (j Mod 2) = 1 Then
row.BgColor = "Gainsboro"
End If
' Iterate through the cells of a row.
For i = 0 To numcells - 1
' Create a new cell and add it to the Cells collection.
cell = New HtmlTableCell()
cell.Controls.Add(New LiteralControl("row " & _
j.ToString() & _
", cell " & _
i.ToString()))
row.Cells.Insert(i, cell)
Next i
Table1.Rows.Insert(j, row)
Next j
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>HtmlTableRowCollection Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>HtmlTableRowCollection Example</h3>
<table id="Table1"
runat="server"
cellspacing="0"
style="padding:5; border-width:1; border-color:Black"/>
<hr />
Select the number of rows and columns to create: <br /><br />
Table rows:
<select id="Select1"
runat="server">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
Table cells:
<select id="Select2"
runat="server">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<br /><br />
<input type="submit"
value="Generate Table"
runat="server"/>
</form>
</body>
</html>
備註
Insert使用 方法,將指定的 HtmlTableRow 物件加入集合 HtmlTableRowCollection 中指定的索引處。 如果您只想要將 附加 HtmlTableRow 至集合結尾,請使用 Add 方法。