473,804 Members | 3,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.W ebControls.Imag eButton

img.ImageUrl = "images/buy.gif"

next


Jul 19 '05 #1
3 5530
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**********@m indspring.com> wrote in message
news:#z******** ******@TK2MSFTN GP10.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.W ebControls.Imag eButton

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.EventArg s) Handles MyBase.Load

If Not IsPostBack Then

For i = 1 To 5

img = New System.Web.UI.W ebControls.Imag eButton

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******** ********@tk2msf tngp13.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**********@m indspring.com> wrote in message
news:#z******** ******@TK2MSFTN GP10.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.W ebControls.Imag eButton

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_TestImage Click

Inherits System.Web.UI.P age

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.

<System.Diagnos tics.DebuggerSt epThrough()> Private Sub InitializeCompo nent()

End Sub

Protected WithEvents TextBox1 As System.Web.UI.W ebControls.Text Box

Protected WithEvents Table1 As System.Web.UI.W ebControls.Tabl e

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

'Do not delete or move it.

Private designerPlaceho lderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArg s) 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.W ebControls.Tabl eRow

Dim td As New System.Web.UI.W ebControls.Tabl eCell

For i = 1 To 5

tr = New System.Web.UI.W ebControls.Tabl eRow

td = New System.Web.UI.W ebControls.Tabl eCell

Dim myLB As LinkButton

Dim obj As System.Object

myLB = New LinkButton

myLB.ID = "myLB" & i.ToString

myLB.CommandNam e = "Command" & i

myLB.CommandArg ument = i

AddHandler myLB.Click, AddressOf DoStuff

myLB.Text = "My Link"

myLB.EnableView State = True

td.Controls.Add (myLB)

tr.Controls.Add (td)

Table1.Controls .Add(tr)

Next

InitializeCompo nent()

End Sub

#End Region

Protected WithEvents myLB As System.Web.UI.W ebControls.Link Button
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load

If Not IsPostBack Then

End If

End Sub

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

Response.Write( "<script language=javasc ript>window.ale rt('It was
called')</script>")

TextBox1.Text = "Button was clicked sender.CommandN ame = " &
sender.CommandN ame & " CommandArg = " & sender.CommandA rgument

End Sub

"Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in
message news:%2******** ********@tk2msf tngp13.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**********@m indspring.com> wrote in message
news:#z******** ******@TK2MSFTN GP10.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.W ebControls.Imag eButton

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
18584
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 } Which is called statically by: <button onclick="doClick(event,this);">Click me</button>
2
2932
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 a WebForm with some textboxes, dropdownlists, a panel, imagebutton and so on. When I click on the image button (which was created at design time) I dynamically build a table. In each of row of that new table I put several cells and one cell...
1
3142
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 elements and would like to seek a solution for this. I had looked through several articles for accessing programatically-created dynamic elements such as: 1)
5
19667
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. Any suggestions? I tried with btnAdd.Attributes.Add("onClick","AddItem();")
3
299
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 help? Thanks in advance. for i = 1 to 10 img = New System.Web.UI.WebControls.ImageButton
9
14469
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 does not. The problem is that the webcontrols get created on the OnLoad event of the usercontrol and the event handlers are assigned at the same time. I have to click twice on the controls for the events to be raised, the first time nothing...
7
3576
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 the button is added to is created at run time? here is code from my aspx page... ------------------------------------------------------------------------------------- <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">...
1
2475
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 website. I've created an AJAX based user control, which is dynamically placed on a page (once, or multiple times) if the user has the correct permissions to receive it. It contains two drop down lists - employee value and partner value; and a...
1
1898
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 OpenNETCF.Windows.Forms.Button2 Me.BtnOperate.Location = New System.Drawing.Point(20, 20) Me.BtnOperate.Size = New System.Drawing.Size(150, 150) Me.BtnOperate.TabIndex = 0 Me.Controls.Add(Me.BtnOperate)
0
9589
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
10593
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
10329
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
9163
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...
1
7626
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6858
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
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3830
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3000
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.