Dynamically Creating Textbox - Object Reference Not Set to an Inst

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?a2VubnltY2U=?=

    #1

    Dynamically Creating Textbox - Object Reference Not Set to an Inst

    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.P age

    Private Sub Page_Init(ByVal sender As Object, ByVal e As
    System.EventArg s) Handles Me.Init
    Dim txtTest As New TextBox
    PlaceHolder1.Co ntrols.Add(txtT est)

    End Sub

    Private Sub Button1_Click(B yVal sender As Object, ByVal e As
    System.EventArg s) Handles Button1.Click

    Dim myTextBox As TextBox
    myTextBox = PlaceHolder1.Fi ndControl("txtT est")
    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

    #2
    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.P age
    >
    Private Sub Page_Init(ByVal sender As Object, ByVal e As
    System.EventArg s) Handles Me.Init
    Dim txtTest As New TextBox
    PlaceHolder1.Co ntrols.Add(txtT est)
    >
    End Sub
    >
    Private Sub Button1_Click(B yVal sender As Object, ByVal e As
    System.EventArg s) Handles Button1.Click
    >
    Dim myTextBox As TextBox
    myTextBox = PlaceHolder1.Fi ndControl("txtT est")
    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

    Comment

    • =?Utf-8?B?a2VubnltY2U=?=

      #3
      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.P age

      Private Sub Page_Init(ByVal sender As Object, ByVal e As
      System.EventArg s) Handles Me.Init
      Dim txtTest As New TextBox
      PlaceHolder1.Co ntrols.Add(txtT est)

      End Sub

      Private Sub Button1_Click(B yVal sender As Object, ByVal e As
      System.EventArg s) Handles Button1.Click

      Dim myTextBox As TextBox
      myTextBox = PlaceHolder1.Fi ndControl("txtT est")
      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
      >

      Comment

      • nick chan

        #4
        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.EventArg s) Handles Me.Init
               
                PlaceHolder1.Co ntrols.Add(txtT est)

            End Sub


            Private Sub Button1_Click(B yVal sender As Object, ByVal e As
        System.EventArg s) Handles Button1.Click

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

            End Sub



        On Jul 2, 3:46 pm, kennymce <kenny...@discu ssions.microsof t.com>
        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.P age
        >
            Private Sub Page_Init(ByVal sender As Object, ByVal e As
        System.EventArg s) Handles Me.Init
                Dim txtTest As New TextBox
                PlaceHolder1.Co ntrols.Add(txtT est)
        >
            End Sub
        >
            Private Sub Button1_Click(B yVal sender As Object, ByVal e As
        System.EventArg s) Handles Button1.Click
        >
                Dim myTextBox As TextBox
                myTextBox = PlaceHolder1.Fi ndControl("txtT est")
                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

        Comment

        Working...