I have an Web application that retrieves some db info and send it out to an html table. All created on the fly in vb code behind. For every post (row) in the db I also fill puts an instance of a Dropdownlist with items read from the db.
That means that for every row I read from the database I read the same db info into the Dropdownlist (see the skeleton example below).
A lot of overhead I think. It must be possible to create the Dropdownlist once before the loop and copy from that instance in the loop. But I don’t get it, how can I change the code to manage that?
//KentA
That means that for every row I read from the database I read the same db info into the Dropdownlist (see the skeleton example below).
A lot of overhead I think. It must be possible to create the Dropdownlist once before the loop and copy from that instance in the loop. But I don’t get it, how can I change the code to manage that?
Code:
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cboCombo2 As DropDownList
Dim i As Integer
Dim SupposedDbRs As Integer = 5
'It must be possible to create my DropDownList ONCE here and copy instances from it into the loop but how?
While i < SupposedDbRs
cboCombo2 = addCombo()
cboCombo2.ID = i
form1.Controls.Add(cboCombo2)
i = i + 1
End While
End Sub
Function addCombo() As DropDownList
Dim cboCombo As New DropDownList
'Some db initializing code here
cboCombo.Items.Add("one") ' Normally posts from a db recordset
cboCombo.Items.Add("two")
cboCombo.Items.Add("three")
cboCombo.Items.Add("four")
Return cboCombo
End Function
End Class
Comment