473,387 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.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
Jul 2 '08 #1
3 2296
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
Jul 2 '08 #2
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
Jul 2 '08 #3
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 <kenny...@discussions.microsoft.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.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
Jul 3 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Eric | last post by:
How can I dynamically assign an event to an element? I have tried : (myelement is a text input) document.getElementById('myelement').onKeyUp = "myfnc(param1,param2,param3)"; ...
6
by: Bhavin | last post by:
Anybody quickly replies it, would be a great help! I try to create dynamic array of textboxes control in ASP.NET(C#). I got the following error while I tried to assign value to ID property of...
2
by: Briand | last post by:
In my previous posting I failed miserably, to give enough information to get my difficulty apparent. So to start again. I want to create textboxes dynamically in VB.NET. This is not a web...
3
by: mikeorb | last post by:
Is there a way to determine the object that a property is in? For example: function MyClass() { ... } MyClass.prototype.myFunc = function() { ... } var obj = new MyClass(); var sameobj =...
11
by: Steven D'Aprano | last post by:
Suppose I create a class with some methods: py> class C: .... def spam(self, x): .... print "spam " * x .... def ham(self, x): .... print "ham * %s" % x .......
2
by: Chad | last post by:
I have a problem that I am desperate to understand. It involves dynamically adding controls to a Table control that is built as a result of performing a database query. I am not looking to...
4
by: sydney.luu | last post by:
Hello, I would greatly appreciate if someone can show me how to dynamically build a Repeater with unknown number of columns at design time. I have looked various threads in this newsgroup,...
3
by: keithb | last post by:
My code dynamically adds template fields to a GridView control. Everything seems to work OK, except when updating, because I haven't found a way to reference the dynamically added textboxes....
1
by: semomaniz | last post by:
I have a form where i have created the form dynamically. First i manually added a panel control to the web page. Then i added another panel dynamically and inside this panel i created tables. I have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.