We help IT Professionals succeed at work.
Get Started

How to get the text property from a dynamically created text box

louise001 asked
Last Modified: 2012-06-27

In my web application users click a button to confirm selection from a drop-down list, and using the selection my code displays revenue figures in an html table and adds dynamically created text boxes to the table which should take input from users. Ideally I'd like to allow users to enter or update figures in any number of text boxes without a postback for each edit, then update the database and postback in the click event of another button.

My problem is that I can't get at the text property of the dynamically created text box. In the attached code the event txtEditRevenueTextChanged doesn't run at all.

I'm using VB and .Net Framework 4.0.

Thanks for any help.


' This is the textchanged event handler for the dynamically created text box and when I change the text and then move off the text box it doesn't execute.

 Protected Sub txtTest_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTest.TextChanged

        Dim s As String = txtTest.Text
        Me.ViewState.Add("strTest", s)

    End Sub

Open in new window

' This is what runs when the button to confirm time period selection is clicked, all works fine and the data is displayed as intended.

 For i As Integer = 0 To tDirectors.Rows.Count - 1

                Dim row_data As New TableRow()
                'row_data.ID = tDirectors.Rows(i)("DirectorID").ToString

                'Dim cell_director_id As New TableCell
                'cell_director_id.Text = tDirectors.Rows(i)("DirectorID")

                Dim cell_name As New TableCell
                cell_name.ID = "DirectorID:" + tDirectors.Rows(i)("DirectorID").ToString
                cell_name.Text = tDirectors.Rows(i)("Director")

For int As Integer = 0 To tMonths.Rows.Count

                    If int < tMonths.Rows.Count Then

                        dvRevenues.RowFilter =
                            "(DirectorID = " & tDirectors.Rows(i)("DirectorID") & ") and (MonthID = " & tMonths.Rows(int)("id").ToString & ") or " &
                            "(DirectorID = 0) and (MonthID = " & tMonths.Rows(int)("id").ToString & ")"

                        For Each row As DataRowView In dvRevenues
                            'cells to show revenues and when applicable a text box for editing the revenue amount
                            Dim cell_revenue As New TableCell
                            'cell_revenue.ID = row("DirectorID").ToString & " " & row("MonthID").ToString
                            If tMonths.Rows(int)("id") = row("MonthID") Then
                                If Not row("Closed") Then
                                    Dim txtEditRevenue As New TextBox()
                                    'txtEditRevenue.AutoPostBack = True
                                    If row("DirectorID") <> 0 Then
                                        txtEditRevenue.ID = "DirectorID:" + row("DirectorID").ToString & "MonthID:" & row("MonthID").ToString
                                        txtEditRevenue.Text = row("Revenue").ToString
                                        Dim dtRow As DataRow = dt.NewRow()
                                        dtRow("MonthID") = row("MonthID")
                                        dtRow("DirectorID") = row("DirectorID")
                                        dtRow("Revenue") = row("Revenue")
                                    ElseIf row("DirectorID") = 0 Then
                                        txtEditRevenue.ID = cell_name.ID + "MonthID:" + row("MonthID").ToString
                                    End If
                                    txtEditRevenue.Width = 50
                                    AddHandler txtEditRevenue.TextChanged, AddressOf txtEditRevenueTextChanged
                                ElseIf row("Closed") Then
                                    cell_revenue.Text = row("Revenue").ToString
                                End If
                            End If


Open in new window

Watch Question
This problem has been solved!
Unlock 1 Answer and 14 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE