Dynamically Creating Textbox - Object Reference Not Set to an Inst

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,
 
N

nick chan

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top