Qzyhwipbsnzad
New Member
I am adding a bunch of image buttons at runtime. I declare one image button in the declarations withevents and then I new it inside a for loop. If I just do one image button, then it works. But when I add several, the click event does not work.<BR><BR>Any ideas?<BR><BR>This is the code:<BR><BR>Public Class showThumbs<BR> Inherits System.Web.UI.Page<BR> Public sourcePage As WebForm2<BR> Protected WithEvents newThumb As System.Web.UI.WebControls.ImageButton<BR> Protected table As System.Web.UI.HtmlControls.HtmlTable<BR> Protected row As System.Web.UI.HtmlControls.HtmlTableRow<BR> Protected form As System.Web.UI.HtmlControls.HtmlForm<BR> Protected col As System.Web.UI.HtmlControls.HtmlTableCell<BR> Private WithEvents testb As System.Web.UI.WebControls.Button<BR>ByVal e As System.Web.UI.ImageClickEventArgs)<BR><BR>#Region " Web Form Designer Generated Code "<BR><BR> 'This call is required by the Web Form Designer.<BR> <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()<BR> form = New System.Web.UI.HtmlControls.HtmlForm()<BR> table = New System.Web.UI.HtmlControls.HtmlTable()<BR> form = Me.FindControl("form1")<BR> form.Method = "Post"<BR> form.Target = "test1.aspx"<BR> testb = New System.Web.UI.WebControls.Button()<BR> form.Controls.Add(table)<BR><BR> End Sub<BR><BR> Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init<BR> 'CODEGEN: This method call is required by the Web Form Designer<BR> 'Do not modify it using the code editor.<BR> InitializeComponent()<BR> End Sub<BR><BR>#End Region<BR><BR> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load<BR> 'Put user code to initialize the page here<BR> If Not Page.IsPostBack Then<BR> sourcepage = CType(context.Handler, WebForm2)<BR><BR><BR> Dim path = Server.HtmlEncode(Request.ApplicationPath)<BR> Dim i, j, inc As Integer<BR> Dim length As Integer = sourcepage.get_HowManyThumbs<BR> Dim numRows As Integer = sourcepage.getRows<BR> Dim numCols As Integer = sourcepage.getColumns<BR> inc = 0<BR><BR> For i = 0 To numRows - 1<BR> row = New System.Web.UI.HtmlControls.HtmlTableRow()<BR> table.Controls.Add(row)<BR> For j = 0 To numCols - 1<BR> col = New System.Web.UI.HtmlControls.HtmlTableCell()<BR><BR> row.Controls.Add(col)<BR> If inc < length Then<BR><BR><BR> newThumb = New System.Web.UI.WebControls.ImageButton()<BR><BR> newThumb.ID = "newThumb" & inc.ToString<BR><BR> newThumb.Attributes.Add("runat", "server")<BR> newThumb.ImageUrl = "/media/newThumb" & inc.ToString<BR> newThumb.Enabled = True<BR> newThumb.ToolTip = sourcepage.getOriginalName(inc) & " - " & sourcepage.getOriginalSize(inc) & " bytes"<BR> newThumb.Visible = True<BR> newThumb.BorderStyle = BorderStyle.Outset<BR> newThumb.CausesValidation = True<BR><BR> col.Controls.Add(newThumb)<BR><BR><BR><BR> inc = inc + 1<BR> End If<BR> Next j<BR> Next i<BR><BR> End If<BR> End Sub<BR> <BR><BR><BR> Private Sub newThumb_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles newThumb.Click<BR> Dim something As Integer = 0<BR> Me.form.Page.Response.BinaryWrite(sourcepage.getOr iginalSize(20))<BR> End Sub<BR><BR>End Class<BR>I answered this question yesterday - did you try it?Thanks, I did, but it did not work. That is why I put the code there. It seems that when you add more than one control like I am doing, the event doesn't fire.Hmmmm.... let me actually test it and I will get back to you.Add this line before the col.Controls.Add(newThumb) line:<BR><BR>AddHandler newThumb.OnClick, AddressOf newThumb_Click<BR>ThanksOk, I hacked your code a bit to make it easier to prototype [used regular buttons instead of image buttons, added event handler that just write button clicked]. It works as planned - you need to modify to suit your needs:<BR><BR>Public Class WebForm1<BR> Inherits System.Web.UI.Page<BR> Public sourcePage As WebForm1<BR> Private newThumb As System.Web.UI.WebControls.Button<BR> Protected table As System.Web.UI.HtmlControls.HtmlTable<BR> Protected row As System.Web.UI.HtmlControls.HtmlTableRow<BR> Protected form As System.Web.UI.HtmlControls.HtmlForm<BR> Protected col As System.Web.UI.HtmlControls.HtmlTableCell<BR> Private testb As System.Web.UI.WebControls.Button<BR><BR>#Region " Web Form Designer Generated Code "<BR><BR> 'This call is required by the Web Form Designer.<BR> <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()<BR> form = New System.Web.UI.HtmlControls.HtmlForm()<BR> table = New System.Web.UI.HtmlControls.HtmlTable()<BR> form = Me.FindControl("form1")<BR> form.Method = "Post"<BR> testb = New System.Web.UI.WebControls.Button()<BR> form.Controls.Add(table)<BR><BR> End Sub<BR><BR> Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init<BR> 'CODEGEN: This method call is required by the Web Form Designer<BR> 'Do not modify it using the code editor.<BR> InitializeComponent()<BR> End Sub<BR><BR>#End Region<BR><BR> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load<BR> sourcePage = CType(context.Handler, WebForm1)<BR> Dim path = Server.HtmlEncode(Request.ApplicationPath)<BR> Dim i, j, inc As Integer<BR> Dim length As Integer = 4<BR> Dim numRows As Integer = 2<BR> Dim numCols As Integer = 2<BR> inc = 0<BR><BR> For i = 0 To numRows - 1<BR> row = New System.Web.UI.HtmlControls.HtmlTableRow()<BR> table.Controls.Add(row)<BR> For j = 0 To numCols - 1<BR> col = New System.Web.UI.HtmlControls.HtmlTableCell()<BR><BR> row.Controls.Add(col)<BR> If inc < length Then<BR> newThumb = New System.Web.UI.WebControls.Button()<BR> newThumb.ID = "newThumb" & inc.ToString<BR> newThumb.Text = "newThumb" & inc.ToString<BR> newThumb.Attributes.Add("runat", "server")<BR> AddHandler newThumb.Click, AddressOf Me.newThumb_Click<BR> col.Controls.Add(newThumb)<BR> newThumb.EnableViewState = True<BR> inc = inc + 1<BR> End If<BR> Next j<BR> Next i<BR> End Sub<BR><BR> Public Sub newThumb_Click(ByVal sender As Object, ByVal e As EventArgs)<BR> Response.Write("Clicked " & sender.ID)<BR> End Sub<BR><BR>End Class<BR>Thanks. I will work with that.Thanks