The following just worked in Visual Web Developer 2005 Express Beta 1. I
haven't yet tried it in VS 2003:
Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArg s)
Handles Me.Load
Dim btn As Button
For i As Integer = 1 To 3
btn = New Button
AddHandler btn.Click, AddressOf Button_Click
pnlButtonContai ner.Controls.Ad d(btn)
If Not Page.IsPostBack Then
btn.Text = String.Format(" Button {0}", i)
End If
Next
End Sub
Sub Button_Click(By Val sender As Object, ByVal e As EventArgs)
Dim btn As Button = DirectCast(send er, Button)
lblResult.Text = String.Format(" Button clicked: {0}", btn.Text)
End Sub
BTW, note the little trick of setting the properties of the button after the
button has been added to the Controls collection of its parent. Property
changes made after the control is part of the control hierarchy are
persisted in ViewState. That's why the code above can get away with only
setting the properties on the first request. You may find this useful for
avoiding re-reading the database.
Note that my code already knows how many controls and of what type it wants
to create. In your case, you're getting that information out of the
database. I believe that you could read that information from the database
on the initial request and store it in, for instance, an ArrayList. You
could then save the ArrayList in the page's ViewState:
Dim specs As New ArrayList
If Not Page.IsPostBack Then
' Fill the Specifications typed DataSet, then:
For Each specRow As SpecificationRo w In
Specifications. SpecificationsT able.Rows
specs.Add(New Pair(specRow.Co ntrolType, specRow.Control Text))
Next
'
ViewState["specs"] = specs
Else
specs = DirectCast(View State["specs"], ArrayList)
End If
'
' Now use specs to create and add the controls you want, and you won't
need the database on postbacks
--
John Saunders
johnwsaundersii i at hotmail
"stb" <si***@intisi.f snet.co.uk> wrote in message
news:uC******** ******@TK2MSFTN GP09.phx.gbl...
Hi, I tried the 'AddHandler' method pointing to a sub allready, and have
now tried it again. But for some reason it does not work.
Any ideas??????
"John Saunders" <jo************ **@notcoldmail. com> wrote in message
news:Ok******** ******@TK2MSFTN GP11.phx.gbl... I've answered inline:
"stb" <si***@intisi.f snet.co.uk> wrote in message
news:OR******** ******@TK2MSFTN GP10.phx.gbl... I have an empty asp:table on a form.
Rows and cells in the rows are added programatically . At the end of
each row, there is a cell with a button inside it.
How do I catch the button's click event?
dim dr as sqlDataReader
Dim tempCell As TableCell
dr=........
While dr.Read
Dim tempRow As New TableRow
Dim i As Integer
For i = 0 To dr.FieldCount - 1
tempCell = New TableCell
tempCell.Text = CStr(dr.GetValu e(i))
tempRow.Cells.A dd(tempCell)
Next i
tempCell = New TableCell
btn = New Button
btn.CommandName = "Edit"
btn.CommandArgu ment = i
btn.Text = "Edit"
AddHandler btn.Click, AddressOf EditButton_Clic k tempCell.Contro ls.add(btn)
tempRow.Cells.A dd(tempCell)
Table1.Rows.Add (tempRow)
End While
...
Private Sub EditButton_Clic k (sender As Object, e As EventArgs)
Dim btn As Button = DirectCast(send er, Button)
...
End Sub
--
John Saunders
johnwsaundersii i at hotmail