Solved

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

Posted on 2011-03-14
5
451 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

696 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