problem while getting values from dynamically created textboxes

 Hi Friends

I have created some text boxes dynamically and I have problem while accessing their changed values. And I am using the following code. 

1    TextBox t =(TextBox) this.form1.FindControl("field1");
2    string name = t.Text;

  When I try to execute the second line i.e. getting values from the text box, it results with Object reference not set to an instance of object.


field1, fiedl2.... all are valid text box ids and they are present in the page.

how to solve this?

12/23/2008 6:12:32 AM 93655 articles. 5 followers. Follow

3 Replies

Similar Articles

[PageSpeed] 9

If you are using master page then field1 is changed to other name. Generally for object reference not set type of errors these two ;ines will not be sufficient to guess actual error.

Also if you are trying this after post back then dyanamic controls no  longer exists on page


Mohan Pathare



12/23/2008 7:41:08 AM

 Thanks for reply

 the vlaues are from database and textboxes are generated according to the fields. And contain data from database.

I have to update and save the data. How this can be handled.


12/23/2008 7:48:43 AM

Hi abidshahzad,

Try using this:


TextBox t = FindControl("field1") as TextBox;


The 'as' keyword performs faster than a cast. Also, you need to retrieve the control from the page, not the form.  This example assumes there is a <asp:TextBox ID='field1" runat="server" /> on your page.

Hope this helps.

Happy Coding,


*** REMINDER ***
If you find this post useful, Please click the 'Mark as Answer' Button.

All Code is provided AS IS.

** Want Some Useful Sites **
12/23/2008 7:56:31 AM

Similar Artilces:

I am having problems getting the info form a textbox created dynamically.
Hello to every one! I am having problems getting the info form a textbox created dynamically. This is what I’m doing. From the click event of a grid I create several textboxes each whith the info a I need to show and with different id’s. This part works fine. They show in the web page. But when I try to get the info out of them after a button click event they are not their any more. They just disappear. Does somebody now why they don’t stay in the code? Items created dynamically will not have viewstate and hence they will not be created automatically on page submit. You can retriev...

problem getting textbox values dynamically
Ok I am trying to loop through a table (tbltest) get all of my textboxes. and assign there .text to a database. Here just to simplify I have been trying to just assign it to a label. here is the code. Dim c As Control For Each c In tbltest.Controls lbltest.Text = c.text Next Problem is it won't let me use .text. anyone know a way around this one. Or am I doing something else wrong? Something like this is probably what you are after: Dim c As Control For Each c In tbltest.Controls 'Is the type of c a TextBox ...

problem with get value from textbox after get it value in load page
hi every one  i have problem with my web site when i get textbox value in load page   and Changed textbox the page do not take last value .   it take value in load page   this is code:Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)   TextBox1.Text = "aa" End SubProtected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) MsgBox(TextBox1.Text) End Sub   when i press button message write "aa" while i changed the textbox   please if enyone knowen help me &nbs...

Dynamically creating textboxes, then getting values
I am learning, but have developed quiet a bit in php. I'm having trouble finding a solution to something really simple in php. Me.lblTracking.Text = "&lt;table><tr><td colspan='3'>Destination Information</td></tr>" Me.lblTracking.Text += "&lt;tr><td>#</td><td>Tracking #</td></tr>" While (i < 4) Me.lblTracking.Text += "&lt;tr>" Me.lblTracking.Text += "&lt;td>Package " & i & ":</td><td><input type='text' name='Tracking[" & i &a...

How to get values from dynamically created textboxes?
I have some code in my code behind file that dynamically generates textboxes according to the number of times a user clicks on a button.When the user enters data into these textboxes,how do i get the data that is from these dynamically generated textboxes? Moose, You can use the below code and the Dynamic IDs of the controls you've created to get the values... ' Variables Dim oTxt As Web.UI.WebControls.TextBox Dim sVar$ ' Assign Control oTxt = Me.FindControl("DynamicControlID") ' Assign Value sVar...

Getting values from dynamically created textboxes
Hello, I'm creating a textboxes boxes dynamically when the user presses a button. I'm then adding the textbox to a placeholder. After the user enters data, I want to get the value that they entered, but I'm having trouble. The second method ReadTextBoxes is also being triggered by a button. The problem is that if they user chooses to had 3 new textboxes in the first method, the placeholder will contain 3 controls, but then when I click the button to read the textboxes, the placeholder is showing zero controls. What am I doing wrong? Thanks// This creates the textb...

Create textbox dynamically but can not get the value?
Hi~~ I had one panel and two buttons on my page. Button1 is for "Create Textbox" , button2 is for "Get values". I got a error message when I click button2. The message is "Object reference not set to an instance of an object."  It can not find the textbox that I created dynaically. Hope someone knows how to fix this?? Thanks all....  Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click        Dim i As Long       ...

Problem with getting the value of a dynamically created CONTROL
Dear,I'm facing a problem with dynamically created controlsI'm creating RadioButtons  according to certain data from a database, the problem is that the RadioButtons are being created and are being displayed in a dynamically created HtmlTable inside the HtmlTableCell. When i want to get the value of the RadioButton if is it checked or not , i'm using the following codeRadioButton rbtn = (RadioButton)Page.FindControl(ID);The rbtn is always null knowing that if i check the Page source i find the RadioButton with the Same ID that I'm calling in the FindControl. Co...

How to get the value of a dynamically created textbox control
Hi,  Can anyone tell me how to get the entered value from a dynamically created textbox apart from getting it from the TextChanged eventhandler? Regards, Vidya Friend, check this thread as similar scenario is discussed over Another one Good luck!Please Don't forget to click "Mark as Answer" on the post that helped you.This can be beneficial to other community members reading the thread.  use Request["TextBoxId"] to get the value entered in it....Also white creating it rememb...

How to get the value of dynamically created text box which is created dynamically on click of button control.
<p>Dear AllI am new in am doing one project there I have one doubt.what I want is I have taken two  button controls in my page, in click of first button1 I have  dynamically created one textbox and I added this textbox to my page and also set the id for this textbox,.after that in click of second button2 I want that the value of the textbox will be print in my page by getting the value of the textbbox. some codes are given below. <form id="form1" runat="server">    <div>      ...

Problem getting dynamically created controls from Form Collection
It seems that any control that I define explicitly in the page is found by the FindControl method, but other controls that are created dynamically in the code behind file are not accessable by that method. I was able to run a test where I create 3 controls dynamically and also place a button server control on the page as well (via the design view in so that a variable gets defined for the button in the code behind class). When I loop though the form collection and write out the type of control (based on if the FindControl method returns nothing or not), I get the button, but not any...

how to get the dynamically created textbox value in code behind?
Hi all, In my page i've a button('add more'). when i click the 'add more' button it will create 4 textboxes and a delete button.on clicking again another 4 textboxes and a delete button will be created and so on. This work well. What my problem is i cannot get the created textbox's value in the server side.. Here is my code..  <%@ Page Language="VB" MasterPageFile="~/SwapnaMaster.master" AutoEventWireup="false" CodeFile="SwapnaTestPage.aspx.vb" Inherits="SwapnaTestPage" title="Untitled Page" ...

Problem: Getting text value of dynamically created TextFields
Hi   I’ve a situation here in which I want to create controls with a button click and then want to retrieve the text value of text field which user will enter. See the simple example application below. When I call Button2_Click the page just go blank. All the dynimacally created controls just got disappear. How can I manage it? can some one please help me?   Please note I just want to create controls once in the application by clicking the Button1_Click.   Thanks!   using System; using System.Data; using System.Configuration; using System.Web; using System....

Creating TextBox Dynamically and Taking Values from them (page load problem)
Hi my friends,My problem is that;I have 1 textBox, 1 create button, 1 save button and 1 listBox. I create textBoxes according to the value that is entered to textBox by the user... I have made the job up to here... Think of entering ' 3 ' for example... We clicked on 'create' button. 3 new textBox was created. User entered some values as a string into that textBoxes. I want that, When user clicked on save button, the values that i have entered, will be listed in listBox  . I couldnt solve this problem because, the textBoxes that i created before, dissappears because of t...

Web resources about - problem while getting values from dynamically created textboxes -

Placebase team at Apple file “Schematic Maps” patent dynamically detailing important data
In a report from Computerworld way back in 2009, we learned that Apple had quietly acquired Maps API company Placebase. Then, founder of Placebase ...

Apple's Lightning port dynamically assigns pins to allow for reversible use
... a closer look at how Apple's new Lightning connector works, and has come to the conclusion that the 8 pins on each side of the plug are dynamically ...

In a dynamically typed language, is it a bad idea to return different data types?
In some cases, returning different types from a single function works.

Urlbox Launches Screenshots-as-a-Service Platform to Dynamically Update Galleries
... changes. Launched yesterday, Urlbox allows anyone with a gallery displaying website screenshots to automate the process and keep them dynamically ... launcher dynamically adapts to what you’re doing right now
... phone according to what you’re doing. Perform a search for ‘football’ and your background will change to the NFL logo. A launcher that dynamically ...

A YouTube Experiment Resizes the Player Dynamically
There's a new YouTube experiment that changes the video player's size, depending on the browser window's size. When resizing Chrome's window, ...

Apple Granted “Dynamically Changing Cursor for User Interface” Patent, Originally Filed in 2003
... a utility patent that covers the use of the mouse cursor that changes according to the context of the task it is engaged in. Called a “Dynamically ...

Atmel's FPSLIC II Dynamically Reconfigurable SoC Supports "Silicon-Sharing" For Peripherals & Interfaces ...
Silicon-Sharing Fits 200K Gate Design into 40K Gate FPSLIC II's, Cuts Power Drain by 97% Launcher Wants to Dynamically Change Phones Based on Location, Interests, and Mental State ...
What if your phone’s launcher dynamically changed based upon interests, your location, or topics you had in mind at a specific moment? A new ...

Resources last updated: 12/17/2015 11:23:54 AM