Dynamically Creating Textbox - Object Reference Not Set to an Inst

Discussion in 'ASP .Net' started by kennymce, Jul 2, 2008.

  1. kennymce

    kennymce Guest

    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,

    --
    Kenny.
    Systems Developer
    kennymce, Jul 2, 2008
    #1
    1. Advertising

  2. kennymce

    nick chan Guest

    Re: Dynamically Creating Textbox - Object Reference Not Set to anInst

    Dim txtTest As New TextBox
    txtText.ID = "txtTest"

    kennymce wrote:
    > 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,
    >
    > --
    > Kenny.
    > Systems Developer
    nick chan, Jul 2, 2008
    #2
    1. Advertising

  3. kennymce

    kennymce Guest

    Re: Dynamically Creating Textbox - Object Reference Not Set to an

    Thank you, Mr Chan. Sorted.
    --
    Kenny.
    Systems Developer


    "nick chan" wrote:

    > Dim txtTest As New TextBox
    > txtText.ID = "txtTest"
    >
    > kennymce wrote:
    > > 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,
    > >
    > > --
    > > Kenny.
    > > Systems Developer

    >
    kennymce, Jul 2, 2008
    #3
  4. kennymce

    nick chan Guest

    Re: Dynamically Creating Textbox - Object Reference Not Set to anInst

    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



    On Jul 2, 3:46 pm, kennymce <>
    wrote:
    > 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,
    >
    > --
    > Kenny.
    > Systems Developer
    nick chan, Jul 3, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UGF0cmljay5PLklnZQ==?=

    System.NullReferenceException: Object reference not set to an inst

    =?Utf-8?B?UGF0cmljay5PLklnZQ==?=, Oct 6, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    3,566
    Ken Dopierala Jr.
    Oct 6, 2004
  2. =?Utf-8?B?UGF0cmljay5PLklnZQ==?=

    System.NullReferenceException: Object reference not set to an inst

    =?Utf-8?B?UGF0cmljay5PLklnZQ==?=, Nov 5, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    508
    =?Utf-8?B?UGF0cmljay5PLklnZQ==?=
    Nov 5, 2004
  3. =?Utf-8?B?QmVu?=
    Replies:
    2
    Views:
    1,936
    =?Utf-8?B?QmVu?=
    Feb 2, 2005
  4. Ron
    Replies:
    3
    Views:
    424
    Steven Cheng[MSFT]
    Oct 8, 2004
  5. Andrew Crossley

    Creating User Controls Dynamically - Object Reference Not Set erro

    Andrew Crossley, Nov 7, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    161
    Andrew Crossley
    Nov 7, 2005
Loading...

Share This Page