Add Events for Dynamically Created Buttons

Examples I've found don't seem to work for me.

Page 1 of 1

3 Replies - 2292 Views - Last Post: 14 January 2010 - 03:38 PM Rate Topic: -----

#1 gymratz   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 129
  • Joined: 18-October 07

Add Events for Dynamically Created Buttons

Post icon  Posted 14 January 2010 - 02:15 PM

My process is as follows:
I access a database table to determine what questions to ask (20-100 questions).
Each question is display along with, possibly, a "done" button.

My code to do this is as follows:

If datareader("Who_Second").ToString = lbl_Your_Position.Text Then
							Dim b1 As New ImageButton
							b1.ID = "btn_second" & datareader("QCC_Q_ID").ToString
							b1.ImageUrl = "~/Workflow/images/followup_yes.jpg"
							AddHandler b1.Click, AddressOf btn_done_clicked
							With MainTable.Rows(i).Cells(6)
								.Controls.Add(b1)
							End With
						End If



What I need to do for each button is strip out what the "QCC_Q_ID" is for that button and insert information into another table of the DB.
My partial code for this is as follows:
Sub btn_done_clicked(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
		Dim MyDate As Date
		Dim QCC_Q_ID, Position As String
		MyDate = Now()
		QCC_Q_ID = e.ToString

		db_connection = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionStringWF"))
		db_connection.Open()
		db_command = New SqlCommand("insert into QCC_As (Client_ID, Eng_ID, QCC_ID, QCC_Q_ID, Who, Date, Position)" & _
														"Values" & _
														"('" & lbl_client_id.Text & "', '" & lbl_eng_id.Text & "', '" & lbl_qcc_id.Text & "', '" & QCC_Q_ID & "', '" & lbl_emp_id.Text & "', '" & MyDate & "', '" & POSITION & "')", db_connection)
		db_command.ExecuteNonQuery()
		'Populate_Page()
	End Sub


Two problems:
1. My sub is never being called, so I can only assume that my "addhandler" is not working.
2. QCC_Q_ID = e.ToString.... That doesn't seem right - is it? What I need to do is get the name of the button (btn_second13) and get just the number out of that (13). Is there any easier way to keep track of that ID?

Is This A Good Question/Topic? 0
  • +

Replies To: Add Events for Dynamically Created Buttons

#2 AMDKilla   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 88
  • Joined: 30-December 09

Re: Add Events for Dynamically Created Buttons

Posted 14 January 2010 - 02:51 PM

Your AddHandler should look like the one below:

			AddHandler b1.Click, AddressOf btn_done_clicked

Private Sub btn_done_clicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
'Code for Fired Event
End Sub


This post has been edited by AMDKilla: 14 January 2010 - 02:53 PM

Was This Post Helpful? 0
  • +
  • -

#3 gymratz   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 129
  • Joined: 18-October 07

Re: Add Events for Dynamically Created Buttons

Posted 14 January 2010 - 02:57 PM

I don't see any difference in your code than mine.
I made the change:
 Sub btn_done_clicked(ByVal sender As Object, ByVal e As System.EventArgs)
		Dim MyDate As Date
		Dim QCC_Q_ID, Position As String
		MyDate = Now()
		QCC_Q_ID = e.ToString

		db_connection = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionStringWF"))
		db_connection.Open()
		db_command = New SqlCommand("insert into QCC_As (Client_ID, Eng_ID, QCC_ID, QCC_Q_ID, Who, Date, Position)" & _
														"Values" & _
														"('" & lbl_client_id.Text & "', '" & lbl_eng_id.Text & "', '" & lbl_qcc_id.Text & "', '" & QCC_Q_ID & "', '" & lbl_emp_id.Text & "', '" & MyDate & "', '" & Position & "')", db_connection)
		db_command.ExecuteNonQuery()
		'Populate_Page()
	End Sub


From System.Web.UI.ImageClickEventArgs to System.EventArgs
No difference... The page posts-back, my code is never executed (I put page-breaks on every line of code within that Sub, and it is never called).

Now, when my page posts-back, all of my labels/buttons/etc that I created are no longer there... Could this be an issue?
Was This Post Helpful? 0
  • +
  • -

#4 AMDKilla   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 88
  • Joined: 30-December 09

Re: Add Events for Dynamically Created Buttons

Posted 14 January 2010 - 03:38 PM

do you have control over the page that you are using? if so, you can call a Sub from javascript, if that helps
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1