473,661 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.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"
tempCell.Contro ls.add(btn)
tempRow.Cells.A dd(tempCell)
Table1.Rows.Add (tempRow)

End While
Nov 18 '05 #1
4 1588
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
Nov 18 '05 #2
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??????

"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

Nov 18 '05 #3
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


Nov 18 '05 #4
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.

"John Saunders" <jo************ **@notcoldmail. com> wrote in message
news:%2******** *******@TK2MSFT NGP11.phx.gbl.. .
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



Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1166
by: Stephen | last post by:
I have the below asp table and I would like to be able to call up the lblAddress1 label with in the asp table in the code behind page. I tried doing this.lblAddress1 and it didn't work and I tried doing this.tblSearchAddresses.lblAddress1 and it didn't work. Has anyone an idea how I call this lable with a asp table. Thanks in advance for any help anyone can give me. <asp:table id="tblSearchAddresses" Runat="server" Width="100%">...
2
2673
by: Kevin | last post by:
I am just learning asp.net and ran into a problem that I have not been able to resolve. I have a web form with an html table that houses an asp:label, asp:textbox and asp:button within. I had the click event of the asp:button working. I then decided to drop an asp:table on the form and then added the three component mentioned above into the asp:table. Now I cannot find a way to set the click even of the asp:button anymore. In fact,...
3
12014
by: Marty McDonald | last post by:
I have <asp:Table... </asp:Table> on my webform. In codebehind, I populate a DataTable whose data should appear in the asp:Table. I created my own code to populate the asp:Table with the DataTable, then I discovered the asp:Table has a DataBind method. But the method takes no args and so I'm confused how to use it. Is there a link to see how DataBind works for asp:Table? Should I just use my own code anyway? Thanks... Here's my...
4
1939
by: coleenholley | last post by:
I asked the question yesterday: > HI All :-) > > I don't know if I will be able to do this type of formatting, but what I > need to do is have a table row where the text wraps (This is easy) the first > line of text is font size 10 pt and the second (wrapped text) is 6 pt. I > know how to create a style sheet to get all of my font one size or the > other, but does any one have any suggestions on how to do this?
1
12255
by: Ed West | last post by:
Hello, How can I put a textbox control into a cell of an asp:Table? I was not able to do it from the properties sheet, so I put it in via html tab, but now I can't select it from the Design tab (nor select it from the Properties tab on the right). Any ideas? <asp:Table id="Table1" runat="server"> <asp:TableRow> <asp:TableCell Text="Email:"></asp:TableCell>
8
3880
by: tatemononai | last post by:
I had a beautiful script that was running, well, just beautifully. But then I decided to take a button that fired an event and place it inside a <asp:table. The event WILL NOT FIRE INSIDE THE TABLE!?! When I move the button outside the table, it works just fine. Inside the table, it doesn't. What gives?
4
3930
by: Nathan Sokalski | last post by:
When editing an ASP Table, Visual Studio does not allow me to edit it in Design View. This makes it harder to add elements, because I must add every element either by using Design View to create the element outside of the table and then using cut & paste in HTML View to move it to the desired location, or by manually typing the code in using HTML View. The first technique sometimes does not automatically update the list of elements...
0
1109
by: Leo Pape | last post by:
Hello, When I place a <asp:table> in a formview (In the edititemtemplate or insertitemtemplate) I'm not able to update data anymore if the bound controls are placed in that table When I place the controls in a general html table it works fine My code that dos not work: <EditItemTemplate>
2
2376
by: clickon | last post by:
I am confused about the way in which asp:table objects work. When a control is within an asp table it generally appears to be in the scope of the tables parent control. E.g. if i have a page that consists of an asp table called MyTable containing a textbox control call MyTextBox then i can access MyTextBox directly from the PageLoad event handler without having to use MyTable.FindControl(MyTextBox). MyTextBox can also be used for a...
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8341
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8542
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8630
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7362
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4177
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4343
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.