2010年12月12日

動態產生LinkButton

這邊的範例會利用table control 動態產生table, 並在cell裡加入LinkButton
首先你在form上加入一個Label1, 程式裡利用兩層loop產生8*4個linkButton‧

LinkButton_Click是用來接受LinkButton事件, 並利用AddHandler lb.Click, AddressOf LinkButton_Click, 把事件加入‧
要取得LinkButton control, 利用Ctype將sender轉型成LInkBUtton
Partial Class LinkButtonTest
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim tbTime As Table = New Table()

Me.Form.Controls.Add(tbTime) 'add table to form


For i As Integer = 1 To 8
Dim r As New TableRow()
For j As Integer = 1 To 3

Dim c As New TableCell() 'declare TableCell
Dim lb As New LinkButton() 'declare LinkBUtton
lb.Text = "LinkButton" + i.ToString() + "-" + j.ToString() 'assign text value of LinkButton
AddHandler lb.Click, AddressOf LinkButton_Click

c.Controls.Add(lb) 'add LinkButton
r.Cells.Add(c) ' add cell to row

Next
tbTime.Rows.Add(r) 'add row to table
Next
End Sub
Protected Sub LinkButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)

Dim lnkBtn As LinkButton

lnkBtn = CType(sender, LinkButton)

Me.Label1.Text = lnkBtn.Text

End Sub


End Class




參考:



http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.table.aspx

沒有留言:

張貼留言