Solved

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

Posted on 2011-03-14
5
445 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
Comment Utility
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
Comment Utility
Hi

How would I do that?

Thanks

Brasso
0
 
LVL 13

Accepted Solution

by:
gamarrojgq earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now