By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,978 Members | 1,467 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,978 IT Pros & Developers. It's quick & easy.

Accessing OnClick event for dynamically created controls.

P: n/a
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


Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
"Jim Mitchell" <ji**********@mindspring.com> wrote in message
news:#z**************@TK2MSFTNGP10.phx.gbl...
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

Jul 19 '05 #2

P: n/a
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


"Alvin Bruney" <vapordan_spam_me_not@hotmail_no_spamhotmail.com > wrote in
message news:%2****************@tk2msftngp13.phx.gbl...
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
"Jim Mitchell" <ji**********@mindspring.com> wrote in message
news:#z**************@TK2MSFTNGP10.phx.gbl...
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


Jul 19 '05 #3

P: n/a
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

"Alvin Bruney" <vapordan_spam_me_not@hotmail_no_spamhotmail.com > wrote in
message news:%2****************@tk2msftngp13.phx.gbl...
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
"Jim Mitchell" <ji**********@mindspring.com> wrote in message
news:#z**************@TK2MSFTNGP10.phx.gbl...
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


Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.