creating labels and textboxes dynamically


Jul 2, 2004
Programming Experience
Dear All,

I am trying to create some labels and textboxes dynamically for an web page using VB.NET

Basically what I want to do is, I have a table which has all the "pages" of my website in it, and I want to loop through that table and create a label and textbox for every entry i find.

At the moment, here is my code:-

Imports System.Configuration
Imports System.Data.OleDb
Imports System.Data.Common.DbDataRecord
Imports System.Web.UI.WebControls.Label
Imports System.Web.UI.WebControls.PlaceHolder
Imports System.Windows.Forms.Form

Public Class PagetitlesNew
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

If Not Me.IsPostBack Then
End If
End Sub

Protected WithEvents Container As System.Web.UI.WebControls.PlaceHolder

Private Sub ConstructEditTable()

'Container.Controls.Add(New LiteralControl("<table>" & vbNewLine))

Dim comp As New Components

Dim i As Integer
Dim dst As New DataSet
Dim strSQL As String
strSQL = "SELECT * FROM updText "
dst = comp.get_dataset(strSQL)
'loop through this dataset
If dst.Tables(0).Rows.Count > 0 Then
For i = 0 To dst.Tables(0).Rows.Count - 1

With Container.Controls
Dim Title As String
Title = dst.Tables(0).Rows(i).Item("UpdTit").ToString()
.Add(New LiteralControl("<tr>" & vbNewLine))

'Label column
.Add(New LiteralControl("<td>" & vbNewLine))
Dim Label = New Label
Label.ID = "lbl" & Title
Label.Text = Title
Label.EnableViewState = True
.Add(New LiteralControl("</td>" & vbNewLine))

'Textbox column
.Add(New LiteralControl("<td>" & vbNewLine))
Dim txtTest = New TextBox
txtTest.ID = "txt" & Title
txtTest.Text = dst.Tables(0).Rows(i).Item("UpdPageTit").ToString()
txtTest.Width = Unit.Pixel(50)
txtTest.EnableViewState = True
.Add(New LiteralControl("</td>" & vbNewLine))
.Add(New LiteralControl("</tr>" & vbNewLine))
End With
Container.Controls.Add(New LiteralControl("</table>" & vbNewLine))
End If
End Sub

End Class

However, when i try and run the project, I have the following error:-

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

can you please help me out?

Top Bottom