Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Dynamically Creating Textbox - Object Reference Not Set to an Inst

Reply
Thread Tools

Dynamically Creating Textbox - Object Reference Not Set to an Inst

 
 
kennymce
Guest
Posts: n/a
 
      07-02-2008
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
 
Reply With Quote
 
 
 
 
nick chan
Guest
Posts: n/a
 
      07-02-2008
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

 
Reply With Quote
 
 
 
 
kennymce
Guest
Posts: n/a
 
      07-02-2008
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

>

 
Reply With Quote
 
nick chan
Guest
Posts: n/a
 
      07-03-2008
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 <(E-Mail Removed)>
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating User Controls Dynamically - Object Reference Not Set erro Andrew Crossley ASP .Net Web Controls 0 11-07-2005 10:26 PM
System.NullReferenceException: Object reference not set to an inst =?Utf-8?B?QmVu?= ASP .Net 2 02-02-2005 05:39 PM
System.NullReferenceException: Object reference not set to an inst =?Utf-8?B?UGF0cmljay5PLklnZQ==?= ASP .Net 0 11-05-2004 05:41 AM
ListItem VALUE -I can not set with code (taking text property inst Ron ASP .Net Web Controls 3 10-08-2004 01:34 AM
System.NullReferenceException: Object reference not set to an inst =?Utf-8?B?UGF0cmljay5PLklnZQ==?= ASP .Net 3 10-06-2004 02:10 AM



Advertisments