Dynamically Created Control

J

Jason MacKenzie

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
 
K

Karl Seguin

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
 
J

Jason MacKenzie

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

Thank you sir.
 
I

Ian Frawley

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

Ian
 
J

Jeffrey Palermo [MCP]

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
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top