473,387 Members | 1,453 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Accessing OnClick event for dynamically created controls.

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
3 5446
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: RobG | last post by:
I am trying to dynamically add an onclick to an element, however I just can't get the syntax right. consider the following function: function doClick (evt,x) { // do things with evt and x } ...
2
by: R Duke | last post by:
I have tried everything I can think of to change the visible property of a design time created control from a dynamically created control's command event handler. Here is the scenario. I have...
1
by: CS Wong | last post by:
Hi, I have a page form where form elements are created dynamically using Javascript instead of programatically at the code-behind level. I have problems accessing the dynamically-created...
5
by: RA | last post by:
I have created a button dynamically; which has been added to a TableCell of a TableRow of a Table control. Is there a way to add onclick event which calls a procedure on the Server-side itself....
3
by: Jim Mitchell | last post by:
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...
9
by: Marcelo Cabrera | last post by:
Hi, I have a user control that in turn creates a bunch of webcontrols dynamically and handles the events these webcontrols raise. It used to work fine on ASP .Net 1.1 but when compiled on 2.0 it...
7
by: rsaffy | last post by:
I am having trouble with my dynamically created button's event handling. I read that the buttons need to be recreated on every trip to the server, but how exactly do you do that when the datagrid...
1
by: AndiSmith | last post by:
Hi guys, I wondered if anyone could help me with this problem, or even shed some light on the direction I need to take to resolve it? I'm using .NET 2.0 (C# flavor) to build a large user-based...
1
by: Grega | last post by:
Hi all, I am trying to create a VB smart device application. I create few buttons dynamically on form_load... something like this: Dim WithEvents BtnOperate As New...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.