Solved

ASP.Net Set The Value of a Dynamically created text box

Posted on 2011-03-14
5
449 Views
Last Modified: 2012-05-11
Hi

I'm trying to create a page which has loads of dynamically created controls.  I need to be able to change the values of some of these and read all of the value to update my MSSQL database.

I've managed to create my controls ok, but now I need to update their values e.g. when a user enters quantity and unit value a total value is also displeayed.

I've tried using the below code to handle the  text changed event, but I get:
Object reference not set to an instance of an object.

Can any on ehelp here?  

Thanks

Brasso





'Control from which the event is fired
Dim cell14 As New TableCell()
Dim txt14 As New TextBox
txt14.ID = "txt_qty_" & reader(0)
txt14.Font.Bold = True
txt14.AutoPostBack = True
txt14.Text = 0
txt14.Width = "40"
cell14.Controls.Add(txt14)
trow2.Cells.Add(cell14)
AddHandler txt14.TextChanged, AddressOf Me.TextBox_TextChanged

'control to update
Dim cell15 As New TableCell()
Dim txt15 As New TextBox
txt15.ID = "txt_sellperu_" & reader(0)
txt15.Text = 0
txt15.Font.Bold = True
txt15.Width = "60"
cell15.Controls.Add(txt15)
trow2.Cells.Add(cell15)

'Text change handler
    Protected Sub TextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim From As TextBox = DirectCast(sender, TextBox)
        Dim ProdType As String = From.ID.Substring((Len(From.ID)) - 1, 1)

        Dim myctnr As TextBox = FindControl("txt_sellperu_" & ProdType)
        myctnr.Text = "Hello"


    End Sub

Open in new window

0
Comment
Question by:brasso_42
  • 2
  • 2
5 Comments
 
LVL 13

Expert Comment

by:gamarrojgq
ID: 35129110
Hi,

You are using FindControl on the Page, that means that  the Textbox txt_sellperu_ will not be found since is inside of a Table, so you have to run the FindControl ON the table.



0
 
LVL 1

Author Comment

by:brasso_42
ID: 35129242
Hi

How would I do that?

Thanks

Brasso
0
 
LVL 13

Accepted Solution

by:
gamarrojgq earned 500 total points
ID: 35129453
ok, in your example, in the first code you are using trow2 to add the cells Dynamically, you have to be adding this row to a table, if that table have an ID look for the Table first like this

Dim tblTable as Table =  DirectCast(FindControl("TheTableID"), Table)

Then Runt the FindControl on the Table

Dim myctnr As TextBox =  tblTable.FindControl("txt_sellperu_" & ProdType)

Just be sure that the variable PRODTYPE is getting the right ID number
0
 
LVL 7

Expert Comment

by:foobarr
ID: 35132385
You could try iterating through all the controls on the page

dim ctrl as control

for each ctrl in Form1.controls

if typeof ctrl is textbox then
 
        Dim ProdType As String = ctrl.ID.Substring((Len(ctrl.ID)) - 1, 1)

        if ctrl.id.equals(("txt_sellperu_" & prodtype)
            ctrl.Text = "Hello"
        end if

end if

next
0
 
LVL 1

Author Comment

by:brasso_42
ID: 35146163
Hi

It was because the control was in a content pleace hodler and then in a update panel.  The fact it was in a table made no differance, but thanks for the pointer gamarrojgg, you put me on the right track so you get the points.

Thanks

Brasso
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question