Catching events from ctls programatically added to a web asp:table

S

stb

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.GetValue(i))
tempRow.Cells.Add(tempCell)
Next i

tempCell = New TableCell
btn = New Button
btn.CommandName = "Edit"
btn.CommandArgument = i
btn.Text = "Edit"
tempCell.Controls.add(btn)
tempRow.Cells.Add(tempCell)
Table1.Rows.Add(tempRow)

End While
 
J

John Saunders

I've answered inline:

stb said:
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.GetValue(i))
tempRow.Cells.Add(tempCell)
Next i

tempCell = New TableCell
btn = New Button
btn.CommandName = "Edit"
btn.CommandArgument = i
btn.Text = "Edit"
AddHandler btn.Click, AddressOf EditButton_Click
tempCell.Controls.add(btn)
tempRow.Cells.Add(tempCell)
Table1.Rows.Add(tempRow)

End While
....
Private Sub EditButton_Click (sender As Object, e As EventArgs)
Dim btn As Button = DirectCast(sender, Button)
...
End Sub
 
S

stb

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??????
 
J

John Saunders

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.EventArgs)
Handles Me.Load
Dim btn As Button
For i As Integer = 1 To 3
btn = New Button

AddHandler btn.Click, AddressOf Button_Click

pnlButtonContainer.Controls.Add(btn)

If Not Page.IsPostBack Then
btn.Text = String.Format("Button {0}", i)
End If
Next
End Sub

Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As Button = DirectCast(sender, 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 SpecificationRow In
Specifications.SpecificationsTable.Rows
specs.Add(New Pair(specRow.ControlType, specRow.ControlText))
Next
'
ViewState["specs"] = specs
Else
specs = DirectCast(ViewState["specs"], ArrayList)
End If
'
' Now use specs to create and add the controls you want, and you won't
need the database on postbacks
 
S

stb

Hi there.

I've tried to put the button properties after the adding to the table cell
control collection, but this didn't work. I even tried adding the
'AddHandler' statement after as well, but still it doesn't work. I haven't
tried your simpler version yet, maybe I'll give that a go now. The other
option I have is to write an Inline template for a datalist control if that
can be done dynamically, tho I favour my original route.

I've even tried keeping an array of all the buttons added and adding the
properties after adding the button to the Table cell's control collection,
adding the cell to the table row and adding the table row to the table, but
it stil doesn't work?????

Simon.



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.EventArgs)
Handles Me.Load
Dim btn As Button
For i As Integer = 1 To 3
btn = New Button

AddHandler btn.Click, AddressOf Button_Click

pnlButtonContainer.Controls.Add(btn)

If Not Page.IsPostBack Then
btn.Text = String.Format("Button {0}", i)
End If
Next
End Sub

Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As Button = DirectCast(sender, 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 SpecificationRow In
Specifications.SpecificationsTable.Rows
specs.Add(New Pair(specRow.ControlType, specRow.ControlText))
Next
'
ViewState["specs"] = specs
Else
specs = DirectCast(ViewState["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
johnwsaundersiii at hotmail



stb said:
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??????
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top