Dynamically Created Control

Discussion in 'ASP .Net' started by Jason MacKenzie, Nov 25, 2004.

  1. I'm trying to capture the text property of a dynamically created textbox on
    the screen.. I can't get this simple example to work. I get "Object
    Reference Not set to instance....." on the Response.Write(text.Text) in the
    Button1.Click event handler.

    I'm sure this is something painfully simple.

    Thanks a lot,

    Jason MacKenzie

    Public Class WebForm1
    Inherits InformetBaseClass.PageFramework

    Dim text As TextBox



    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.
    InitializeComponent()

    Dim text = New TextBox
    CType(Me.FindControl("Form1"), HtmlForm).Controls.Add(text)
    End Sub



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles Button1.Click
    Response.Write(text.Text)
    End Sub

    End Class
     
    Jason MacKenzie, Nov 25, 2004
    #1
    1. Advertising

  2. Jason MacKenzie

    Karl Seguin Guest

    Public class WebForm1
    inerhits Page

    dim text as TextBox

    sub init
    dim text as new TextBox
    Controls.Add(text)
    end init

    sub click
    response.write(text.text)
    end sub
    end class

    the text variable at the top is never assigned. Because in init you create
    another text variable which is scoped to the function. Simply remove the dim
    text as new TExtbox and replace it with text = new TextBox in init and
    voila.

    KArl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "Jason MacKenzie" <> wrote in message
    news:...
    > I'm trying to capture the text property of a dynamically created textbox

    on
    > the screen.. I can't get this simple example to work. I get "Object
    > Reference Not set to instance....." on the Response.Write(text.Text) in

    the
    > Button1.Click event handler.
    >
    > I'm sure this is something painfully simple.
    >
    > Thanks a lot,
    >
    > Jason MacKenzie
    >
    > Public Class WebForm1
    > Inherits InformetBaseClass.PageFramework
    >
    > Dim text As TextBox
    >
    >
    >
    > 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.
    > InitializeComponent()
    >
    > Dim text = New TextBox
    > CType(Me.FindControl("Form1"), HtmlForm).Controls.Add(text)
    > End Sub
    >
    >
    >
    > Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles Button1.Click
    > Response.Write(text.Text)
    > End Sub
    >
    > End Class
    >
    >
     
    Karl Seguin, Nov 25, 2004
    #2
    1. Advertising

  3. You know Karl, I knew it was something dumb but I could not have pictured it
    being THAT dumb.

    Thank you sir.

    "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
    wrote in message news:...
    > Public class WebForm1
    > inerhits Page
    >
    > dim text as TextBox
    >
    > sub init
    > dim text as new TextBox
    > Controls.Add(text)
    > end init
    >
    > sub click
    > response.write(text.text)
    > end sub
    > end class
    >
    > the text variable at the top is never assigned. Because in init you
    > create
    > another text variable which is scoped to the function. Simply remove the
    > dim
    > text as new TExtbox and replace it with text = new TextBox in init and
    > voila.
    >
    > KArl
    >
    > --
    > MY ASP.Net tutorials
    > http://www.openmymind.net/
    >
    >
    > "Jason MacKenzie" <> wrote in message
    > news:...
    >> I'm trying to capture the text property of a dynamically created textbox

    > on
    >> the screen.. I can't get this simple example to work. I get "Object
    >> Reference Not set to instance....." on the Response.Write(text.Text) in

    > the
    >> Button1.Click event handler.
    >>
    >> I'm sure this is something painfully simple.
    >>
    >> Thanks a lot,
    >>
    >> Jason MacKenzie
    >>
    >> Public Class WebForm1
    >> Inherits InformetBaseClass.PageFramework
    >>
    >> Dim text As TextBox
    >>
    >>
    >>
    >> 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.
    >> InitializeComponent()
    >>
    >> Dim text = New TextBox
    >> CType(Me.FindControl("Form1"), HtmlForm).Controls.Add(text)
    >> End Sub
    >>
    >>
    >>
    >> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
    >> System.EventArgs) Handles Button1.Click
    >> Response.Write(text.Text)
    >> End Sub
    >>
    >> End Class
    >>
    >>

    >
    >
     
    Jason MacKenzie, Nov 25, 2004
    #3
  4. lol
     
    Franck Quintana, Nov 25, 2004
    #4
  5. Jason MacKenzie

    Ian Frawley Guest

    Re-create it in the LoadViewstate event and then the View state will be
    applied to it.

    Ian
    "Jason MacKenzie" <> wrote in message
    news:...
    > I'm trying to capture the text property of a dynamically created textbox

    on
    > the screen.. I can't get this simple example to work. I get "Object
    > Reference Not set to instance....." on the Response.Write(text.Text) in

    the
    > Button1.Click event handler.
    >
    > I'm sure this is something painfully simple.
    >
    > Thanks a lot,
    >
    > Jason MacKenzie
    >
    > Public Class WebForm1
    > Inherits InformetBaseClass.PageFramework
    >
    > Dim text As TextBox
    >
    >
    >
    > 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.
    > InitializeComponent()
    >
    > Dim text = New TextBox
    > CType(Me.FindControl("Form1"), HtmlForm).Controls.Add(text)
    > End Sub
    >
    >
    >
    > Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles Button1.Click
    > Response.Write(text.Text)
    > End Sub
    >
    > End Class
    >
    >
     
    Ian Frawley, Nov 25, 2004
    #5
  6. Jason,
    Just get rid of the Dim in your Init code. Like the following:
    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Init

    InitializeComponent()

    text = New TextBox

    CType(Me.FindControl("Form1"), HtmlForm).Controls.Add(text)

    End Sub



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

    Response.Write(text.Text)

    End Sub


    Best regards,
    Jeffrey Palermo

    "Jason MacKenzie" <> wrote in message
    news:...
    > I'm trying to capture the text property of a dynamically created textbox

    on
    > the screen.. I can't get this simple example to work. I get "Object
    > Reference Not set to instance....." on the Response.Write(text.Text) in

    the
    > Button1.Click event handler.
    >
    > I'm sure this is something painfully simple.
    >
    > Thanks a lot,
    >
    > Jason MacKenzie
    >
    > Public Class WebForm1
    > Inherits InformetBaseClass.PageFramework
    >
    > Dim text As TextBox
    >
    >
    >
    > 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.
    > InitializeComponent()
    >
    > Dim text = New TextBox
    > CType(Me.FindControl("Form1"), HtmlForm).Controls.Add(text)
    > End Sub
    >
    >
    >
    > Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles Button1.Click
    > Response.Write(text.Text)
    > End Sub
    >
    > End Class
    >
    >
     
    Jeffrey Palermo [MCP], Nov 25, 2004
    #6
    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. Duncan Welch
    Replies:
    2
    Views:
    424
    Duncan Welch
    Jul 2, 2003
  2. DesignerX
    Replies:
    2
    Views:
    469
    Teemu Keiski
    Jul 24, 2003
  3. Karl Seguin
    Replies:
    1
    Views:
    590
    MS News \(MS ILM\)
    Aug 10, 2003
  4. John Crowley
    Replies:
    0
    Views:
    745
    John Crowley
    Jan 14, 2004
  5. msimmons
    Replies:
    0
    Views:
    544
    msimmons
    Jul 16, 2009
Loading...

Share This Page