Solved

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

Posted on 2011-03-14
5
448 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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