How to handle events of dynamically created buttons???
-
I'm dynamically updating a web forms table control (tblPics). I'm adding a web forms ImageControl to each cells. The problem is that the function "DisplayImage" is never called when I press one of the images (see Addhandler in the code). The rest works perfectly. Thanks. Here's a part of my code: Private Sub Page_Load ... If Not Page.IsPostBack Then InitPage() End If End Sub Private Sub InitPage() Dim _ImageButton As ImageButton For j = 0 To _Rows - 1 Dim r As New TableRow Dim i As Integer For i = 0 To _Cols - 1 Dim c As New TableCell _ImageButton = New ImageButton _ImageButton.ID = "IB" & Ind.ToString _ImageButton.CommandName = "Display" _ImageButton.CommandArgument = _FileList(Ind) _ImageButton.ImageUrl = _FileList(Ind) AddHandler _ImageButton.Command, AddressOf DisplayImage c.HorizontalAlign = HorizontalAlign.Center c.BorderStyle = BorderStyle.Double c.Controls.Add(_ImageButton) r.Cells.Add(c) Ind = Ind + 1 If Ind = _FileList.Count Then bExit = True Exit For End If Next i tblPics.Rows.Add(r) If bExit Then Exit For End If Next j tblPics.BorderStyle = BorderStyle.None end sub Private Sub DisplayImage(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs) lblTitle.Text = "test" End Sub
-
I'm dynamically updating a web forms table control (tblPics). I'm adding a web forms ImageControl to each cells. The problem is that the function "DisplayImage" is never called when I press one of the images (see Addhandler in the code). The rest works perfectly. Thanks. Here's a part of my code: Private Sub Page_Load ... If Not Page.IsPostBack Then InitPage() End If End Sub Private Sub InitPage() Dim _ImageButton As ImageButton For j = 0 To _Rows - 1 Dim r As New TableRow Dim i As Integer For i = 0 To _Cols - 1 Dim c As New TableCell _ImageButton = New ImageButton _ImageButton.ID = "IB" & Ind.ToString _ImageButton.CommandName = "Display" _ImageButton.CommandArgument = _FileList(Ind) _ImageButton.ImageUrl = _FileList(Ind) AddHandler _ImageButton.Command, AddressOf DisplayImage c.HorizontalAlign = HorizontalAlign.Center c.BorderStyle = BorderStyle.Double c.Controls.Add(_ImageButton) r.Cells.Add(c) Ind = Ind + 1 If Ind = _FileList.Count Then bExit = True Exit For End If Next i tblPics.Rows.Add(r) If bExit Then Exit For End If Next j tblPics.BorderStyle = BorderStyle.None end sub Private Sub DisplayImage(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs) lblTitle.Text = "test" End Sub
-
You'll have to add (+=) the onclick event when you create the button and destroy (-=) it when you remove the button.
I don't really understand what you're explaining ... can you give an example of what I should be using in my code. Thanks.
-
I don't really understand what you're explaining ... can you give an example of what I should be using in my code. Thanks.
I am using C# as an example. When you create a new button. Button button = new Button(); button.Click += new System.EventHandler(button_OnClick); button_Onclick will be your click handler. Before you remove the button, you should remove the event handler by using the button.Click -= new System.EventHandler(button_OnClick);
-
I am using C# as an example. When you create a new button. Button button = new Button(); button.Click += new System.EventHandler(button_OnClick); button_Onclick will be your click handler. Before you remove the button, you should remove the event handler by using the button.Click -= new System.EventHandler(button_OnClick);
button.click .... you cannot do that in VB.NET! I'm using this but the displayimage event handler never fires: AddHandler _ImageButton.Command, AddressOf DisplayImage
-
button.click .... you cannot do that in VB.NET! I'm using this but the displayimage event handler never fires: AddHandler _ImageButton.Command, AddressOf DisplayImage
I didn't realise the line is adding the event handler in vb.net. :) Does the control has AutoPostback=true and the page is posted back to server when you click the button? -- modified at 12:34 Saturday 25th February, 2006 Sorry, 'Autopostback=true' should be 'runat="server"'
-
I didn't realise the line is adding the event handler in vb.net. :) Does the control has AutoPostback=true and the page is posted back to server when you click the button? -- modified at 12:34 Saturday 25th February, 2006 Sorry, 'Autopostback=true' should be 'runat="server"'
i'v added to my code: _ImageButton.EnableViewState = True _ImageButton.CausesValidation = True but it still does not work. Other buttons on the same page work perfectly. Thanks. -- modified at 12:42 Saturday 25th February, 2006
-
I'm dynamically updating a web forms table control (tblPics). I'm adding a web forms ImageControl to each cells. The problem is that the function "DisplayImage" is never called when I press one of the images (see Addhandler in the code). The rest works perfectly. Thanks. Here's a part of my code: Private Sub Page_Load ... If Not Page.IsPostBack Then InitPage() End If End Sub Private Sub InitPage() Dim _ImageButton As ImageButton For j = 0 To _Rows - 1 Dim r As New TableRow Dim i As Integer For i = 0 To _Cols - 1 Dim c As New TableCell _ImageButton = New ImageButton _ImageButton.ID = "IB" & Ind.ToString _ImageButton.CommandName = "Display" _ImageButton.CommandArgument = _FileList(Ind) _ImageButton.ImageUrl = _FileList(Ind) AddHandler _ImageButton.Command, AddressOf DisplayImage c.HorizontalAlign = HorizontalAlign.Center c.BorderStyle = BorderStyle.Double c.Controls.Add(_ImageButton) r.Cells.Add(c) Ind = Ind + 1 If Ind = _FileList.Count Then bExit = True Exit For End If Next i tblPics.Rows.Add(r) If bExit Then Exit For End If Next j tblPics.BorderStyle = BorderStyle.None end sub Private Sub DisplayImage(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs) lblTitle.Text = "test" End Sub
Hai Try out this one
Private Sub Page_Load
InitPage()
End subWhile working with dynamic controls you need to build controls on each post back. Its better to build dynamic controls in
Page_Init
Private Sub Page_Init ....
InitPage()
End Sub