Dynamically Creating Textbox - Object Reference Not Set to an Inst

  • Thread starter Thread starter kennymce
  • Start date Start date
K

kennymce

Hi,

I'm probably being somewhat dim here and missing something obvious, but I'm
trying to dynamically create a text box and then retrieve its value after
postback.

Partial Public Class _Default
Inherits System.Web.UI.Page

Private Sub Page_Init(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Init
Dim txtTest As New TextBox
PlaceHolder1.Controls.Add(txtTest)

End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim myTextBox As TextBox
myTextBox = PlaceHolder1.FindControl("txtTest")
Response.Write("Value for dynamic textbox = " & myTextBox.Text)

End Sub
End Class

I'm getting an "Object Reference Not Set to an Instance" error on the line:

Response.Write("Value for dynamic textbox = " & myTextBox.Text)

It seems that the FindControl function isn't able to find my textbox.

Please can somebody help?

Thanks in advance,
 
if im not mistaken, you can also do this

Dim txtTest As New TextBox
    Private Sub Page_Init(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Init
       
        PlaceHolder1.Controls.Add(txtTest)

    End Sub


    Private Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click

        Response.Write("Value for dynamic textbox = " & txtTest.Text)

    End Sub
 
Back
Top