Accessing OnClick event for dynamically created controls.

J

Jim Mitchell

I have some code behind that generates 10 imagebutton controls.... I can
not seem to figure out how to trap the onclick event for each image and
determine which image was clicked.

Can someone help?

Thanks in advance.
for i = 1 to 10

img = New System.Web.UI.WebControls.ImageButton

img.ImageUrl = "images/buy.gif"

next
 
A

Alvin Bruney

you need to be hooking up your onclick event to its handler. this isn't
generated for you automatically.
this.onclick += onclick handler goes here
 
J

Jim Mitchell

Thanks for the quick response, but I do not know what you mean by....
this.onclick += onclick handler goes here

I created a test page as shown..... Where would I add this.onclick +=
onclick

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If Not IsPostBack Then

For i = 1 To 5

img = New System.Web.UI.WebControls.ImageButton

img.ImageUrl = "images/meeting.gif"

img.ID = "imgEdit"

Next

End If

End Sub
 
J

Jim Mitchell

Thanks for you help pointing me in the right direction. I did finally get
things working. I needed the VB commands, I needed to put the code in the
Init section of the code behind and I needed to stop doing a few stupid
things in my code.

Anyway, I have posted the page below for anyone that might be interested.

Jim



Public Class SLNet_TestImageClick

Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox

Protected WithEvents Table1 As System.Web.UI.WebControls.Table



'NOTE: The following placeholder declaration is required by the Web Form
Designer.

'Do not delete or move it.

Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init

'CODEGEN: This method call is required by the Web Form Designer

'Do not modify it using the code editor.

Dim i As Integer

Dim tr As New System.Web.UI.WebControls.TableRow

Dim td As New System.Web.UI.WebControls.TableCell

For i = 1 To 5

tr = New System.Web.UI.WebControls.TableRow

td = New System.Web.UI.WebControls.TableCell

Dim myLB As LinkButton

Dim obj As System.Object

myLB = New LinkButton

myLB.ID = "myLB" & i.ToString

myLB.CommandName = "Command" & i

myLB.CommandArgument = i

AddHandler myLB.Click, AddressOf DoStuff

myLB.Text = "My Link"

myLB.EnableViewState = True

td.Controls.Add(myLB)

tr.Controls.Add(td)

Table1.Controls.Add(tr)



Next

InitializeComponent()

End Sub

#End Region

Protected WithEvents myLB As System.Web.UI.WebControls.LinkButton


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If Not IsPostBack Then

End If

End Sub



Private Sub DoStuff(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles myLB.Click

Response.Write("<script language=javascript>window.alert('It was
called')</script>")

TextBox1.Text = "Button was clicked sender.CommandName = " &
sender.CommandName & " CommandArg = " & sender.CommandArgument

End Sub
 

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,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top