動態產生表格及欄劫
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
Dim oTable As New Table()
Dim oRow As TableRow
Dim oCell As TableCell
Dim oButton As Button
Dim oTextBox As TextBox
Dim N1 As Integer
Me.Form.Controls.Add(oTable)
'建立3列1欄的,其中第1欄置放 Button,第2欄置放 TextBox
For N1 = 1 To 3
oRow = New TableRow()
oTable.Rows.Add(oRow)
oCell = New TableCell()
oRow.Cells.Add(oCell)
oButton = New Button
oButton.Text = "Button" & N1.ToString()
'將 Button 的 Click 事件導向 Button_Click 函式
AddHandler oButton.Click, AddressOf Button_Click
oCell.Controls.Add(oButton)
oCell = New TableCell()
oRow.Cells.Add(oCell)
oTextBox = New TextBox()
oTextBox.Text = "TextBox" & N1.ToString()
oCell.Controls.Add(oTextBox)
Next
End Sub
'Table 中所有的 Button 的 Click 事件導向函數
Protected Sub Button_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim oButton As Button
Dim oRow As TableRow
Dim oCell As TableCell
Dim oTextBox As TextBox = Nothing
oButton = CType(sender, Button)
oCell = CType(oButton.Parent, TableCell)
oRow = CType(oCell.Parent, TableRow)
'取得內含 TextBox 的 Cell,即第2欄
oTextBox = CType(oRow.Cells(1).Controls(0), TextBox)
oTextBox.Text = oButton.Text
End Sub
End Class
Protected
Protected