I'm trying to generate a grid of 1x1 pixels Images in .NET using VB.NET, using this code:
For i = 1 To 500 Dim img As New ImageButton img.ID = "ibtn" + i.ToString img.ImageUrl = "images/design/click.gif" form1.FindControl("upperpanel").Controls.Add(img) Next
Problem is that it takes a very long time to generate the HTML when I'm running this on page_load event. Can anyone help me out to speed up the rendering of the controls.
Also, I want to add Click event on each image button. How can I do that ??
Thanks
Ok, first thing to do is get the panel control once. Second, create your click method, and then add your handler. This needs to be done prior to the page load event. Use the page_Init event for creating your buttons when you need to add a handler.
Private Sub WebForm1_Init(sender As Object, e As EventArgs) Handles Me.Init
Dim tempPanel As Panel = form1.FindControl("upperpanel")
If tempPanel Is Nothing Then
Return
End If
For i = 1 To 500
Dim img As New ImageButton
img.ID = "ibtn" + i.ToString
img.ImageUrl = "images/design/click.gif"
AddHandler img.Click, AddressOf ImageButton_Click
tempPanel.Controls.Add(img)
Next
End Sub
Protected Sub ImageButton_Click(sender As Object, e As ImageClickEventArgs)
Dim tempImageButton As ImageButton = CType(sender, ImageButton)
Select Case tempImageButton.ID
Case "ibtn1"
'blah
Case Else
End Select
End Sub
As far as performance goes, getting the panel control up front will improve performance. Probably not much. Creating 500 buttons is going to take some time.