?
Solved

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

Posted on 2011-03-14
5
Medium Priority
?
453 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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

762 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