Add control in loop

  • Thread starter Thread starter Steven K
  • Start date Start date
S

Steven K

Hello,

I am creating a label and populating it with HTML. I also need to add a new
textbox for each row. I am having a problem adding the control in the loop.
Any help with this would be appreciated.

Sub Page_Load(Sender As Object, E As EventArgs)
....

Do While spQstName.Read
Dim tbxQstName As New Textbox()
tbxQstName.ID = "textbox" & spQstName("strQuestionNo")
lblQuestion.Text &= "<tr><td >& Controls.Add(tbxQstName) & "</td></tr>"
Loop

End Sub
 
Hi Steven,

The easier way is to drop a Placeholder control onto your page and add your
controls to it in the loop. Also, you might find that the literal control is
an easier/lighter way to create your table and cell markup than the label.

Here's a little sample:

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim mySelectQuery As String = _
"SELECT OrderID, CustomerID FROM Orders"
Dim myCommand As New SqlCommand _
(mySelectQuery, SqlConnection1)
Dim tbxQstName As TextBox
Dim litTags As Literal
Dim spQstName As SqlDataReader
SqlConnection1.Open()
spQstName = myCommand.ExecuteReader()
litTags = New Literal
litTags.Text = "<table>"
PlaceHolder1.Controls.Add(litTags)
Do While spQstName.Read
tbxQstName = New TextBox
litTags = New Literal
litTags.Text = "<tr><td>"
PlaceHolder1.Controls.Add(litTags)
tbxQstName.ID = "textbox" & spQstName("OrderID")
PlaceHolder1.Controls.Add(tbxQstName)
litTags = New Literal
litTags.Text = "</td></tr>" & vbCrLf
PlaceHolder1.Controls.Add(litTags)
Loop
litTags = New Literal
litTags.Text = "</table>"
PlaceHolder1.Controls.Add(litTags)
spQstName.Close()
' Close the connection
SqlConnection1.Close()
End Sub

Does this help?

Ken
Microsoft MVP [ASP.NET]
 
Ken,

Thank you very much. I was able to create the textboxes. One last
question, what does Handles MyBase.Load mean in the following?

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load

Thanks again, Steven


Ken Cox said:
Hi Steven,

The easier way is to drop a Placeholder control onto your page and add your
controls to it in the loop. Also, you might find that the literal control is
an easier/lighter way to create your table and cell markup than the label.

Here's a little sample:

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim mySelectQuery As String = _
"SELECT OrderID, CustomerID FROM Orders"
Dim myCommand As New SqlCommand _
(mySelectQuery, SqlConnection1)
Dim tbxQstName As TextBox
Dim litTags As Literal
Dim spQstName As SqlDataReader
SqlConnection1.Open()
spQstName = myCommand.ExecuteReader()
litTags = New Literal
litTags.Text = "<table>"
PlaceHolder1.Controls.Add(litTags)
Do While spQstName.Read
tbxQstName = New TextBox
litTags = New Literal
litTags.Text = "<tr><td>"
PlaceHolder1.Controls.Add(litTags)
tbxQstName.ID = "textbox" & spQstName("OrderID")
PlaceHolder1.Controls.Add(tbxQstName)
litTags = New Literal
litTags.Text = "</td></tr>" & vbCrLf
PlaceHolder1.Controls.Add(litTags)
Loop
litTags = New Literal
litTags.Text = "</table>"
PlaceHolder1.Controls.Add(litTags)
spQstName.Close()
' Close the connection
SqlConnection1.Close()
End Sub

Does this help?

Ken
Microsoft MVP [ASP.NET]


Steven K said:
Hello,

I am creating a label and populating it with HTML. I also need to add a
new
textbox for each row. I am having a problem adding the control in the
loop.
Any help with this would be appreciated.

Sub Page_Load(Sender As Object, E As EventArgs)
...

Do While spQstName.Read
Dim tbxQstName As New Textbox()
tbxQstName.ID = "textbox" & spQstName("strQuestionNo")
lblQuestion.Text &= "<tr><td >& Controls.Add(tbxQstName) & "</td></tr>"
Loop

End Sub
 
MyBase.Load is the event that fires when the page loads:

"The MyBase keyword behaves like an object variable referring to the base
class of the current instance of a class. MyBase is commonly used to access
base class members that are overridden or shadowed in a derived class. In
particular, MyBase.New is used to explicitly call a base class constructor
from a derived class constructor."

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vakeymybase.asp

Steven K said:
Ken,

Thank you very much. I was able to create the textboxes. One last
question, what does Handles MyBase.Load mean in the following?

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load

Thanks again, Steven


Ken Cox said:
Hi Steven,

The easier way is to drop a Placeholder control onto your page and add your
controls to it in the loop. Also, you might find that the literal control is
an easier/lighter way to create your table and cell markup than the
label.

Here's a little sample:

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim mySelectQuery As String = _
"SELECT OrderID, CustomerID FROM Orders"
Dim myCommand As New SqlCommand _
(mySelectQuery, SqlConnection1)
Dim tbxQstName As TextBox
Dim litTags As Literal
Dim spQstName As SqlDataReader
SqlConnection1.Open()
spQstName = myCommand.ExecuteReader()
litTags = New Literal
litTags.Text = "<table>"
PlaceHolder1.Controls.Add(litTags)
Do While spQstName.Read
tbxQstName = New TextBox
litTags = New Literal
litTags.Text = "<tr><td>"
PlaceHolder1.Controls.Add(litTags)
tbxQstName.ID = "textbox" & spQstName("OrderID")
PlaceHolder1.Controls.Add(tbxQstName)
litTags = New Literal
litTags.Text = "</td></tr>" & vbCrLf
PlaceHolder1.Controls.Add(litTags)
Loop
litTags = New Literal
litTags.Text = "</table>"
PlaceHolder1.Controls.Add(litTags)
spQstName.Close()
' Close the connection
SqlConnection1.Close()
End Sub

Does this help?

Ken
Microsoft MVP [ASP.NET]


Steven K said:
Hello,

I am creating a label and populating it with HTML. I also need to add
a
new
textbox for each row. I am having a problem adding the control in the
loop.
Any help with this would be appreciated.

Sub Page_Load(Sender As Object, E As EventArgs)
...

Do While spQstName.Read
Dim tbxQstName As New Textbox()
tbxQstName.ID = "textbox" & spQstName("strQuestionNo")
lblQuestion.Text &= "<tr><td >& Controls.Add(tbxQstName) & "</td></tr>"
Loop

End Sub
 
Back
Top