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

louise001
louise001 used Ask the Experts™
on
Hello,

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.

Louise


' 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")
                'row_data.Cells.Add(cell_director_id)

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

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")
                                        dt.Rows.Add(dtRow)
                                    ElseIf row("DirectorID") = 0 Then
                                        txtEditRevenue.ID = cell_name.ID + "MonthID:" + row("MonthID").ToString
                                    End If
                                    txtEditRevenue.Width = 50
                                    cell_revenue.Controls.Add(txtEditRevenue)
                                    AddHandler txtEditRevenue.TextChanged, AddressOf txtEditRevenueTextChanged
                                ElseIf row("Closed") Then
                                    cell_revenue.Text = row("Revenue").ToString
                                End If
                            End If
                            row_data.Cells.Add(cell_revenue)
                        Next

Next
Next

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Alpesh PatelSolution Architect at TCS (SAFe®Agilist, TOGAF® Foundation, PSM I®, MCP, MCSD)

Commented:
Please find control using foreach of Page,Controls. for TextBox
Do you have a method with address = txtEditRevenueTextChanged

The address specified in your AddHandler line must exist.

AddHandler txtEditRevenue.TextChanged, AddressOf txtTest_TextChanged

Author

Commented:
UnifiedIS:

I do have that method (see below), but I mistakenly pasted another method in my question.

Louise

 Private Sub txtEditRevenueTextChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        Try

            Dim txt As TextBox = DirectCast(sender, TextBox)
            Response.Write(txt.Text + " ")

        Catch ex As Exception
            Response.Write(ex.ToString)
        End Try

    End Sub
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Author

Commented:
PatelAlpesh:

Use Page.FindControl in what event?

Louise
Hey mate, i'm not sure about the problem ur facing, but this example i tested worked fine.
 
protected void Page_Load(object sender, EventArgs e)
        {
            var textBox = new TextBox();
            textBox.AutoPostBack = true;                
            Page.Form.Controls.Add(textBox);
            textBox.TextChanged += new EventHandler(textBox_TextChanged);
        }

        void textBox_TextChanged(object sender, EventArgs e)
        {
            Response.Write("xxx");
        }

Open in new window

Hope it helps.
Boccio
Just try to create the button and add the TextChanged handle everytime you postback the page. Guess you'll need to have a global TextBox variable to use it in the Page_Load and in the method you do the "for each"
Hope it helps.
Boccio.

Author

Commented:
Hi Boccio,

Thanks for responding. On every postback the text boxes and a corresponding TextChanged event handler are created, and what happens is that the TextChanged event doesn't run which I guess is because each text box is of course newly created in the postback ...

What I really, really need is a way of capturing users' text input into the text boxes which are created at runtime.

Not sure what you mean by a global TextBox variable, please forgive my ignorance. Could you post an example?

Thanks,

Louise
Hey louise, sorry for the late response.
I don't know if you still need it, but here's a small example of what i was talking about:
 
public TextBox textBox { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            textBox = new TextBox();
            textBox.AutoPostBack = true;                
            Page.Form.Controls.Add(textBox);
            textBox.TextChanged += new EventHandler(textBox_TextChanged);
        }

        void textBox_TextChanged(object sender, EventArgs e)
        {
            Response.Write(textBox.Text);
        }

Open in new window

Hope it helps! Just let me know if it's not what you really need...
Boccio

Author

Commented:
Hi Boccio,

Yes still trying to get it to work. As I said before, I instantiate my text boxes at runtime, assign the TextChanged event to them but the TextChanged event just doesn't run.

Louise
If Not row("Closed") Then
Dim txtEditRevenue As New TextBox()
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")
dt.Rows.Add(dtRow)
'txtEditRevenue.ID = cell_name.ID + "MonthID:" + row("MonthID").ToString
txtEditRevenue.Width = 50
Page.Form.Controls.Add(txtEditRevenue)
cell_revenue.Controls.Add(txtEditRevenue)
txtEditRevenue.AutoPostBack = True
AddHandler txtEditRevenue.TextChanged, AddressOf txtEditRevenueTextChanged
                                    ElseIf row("Closed") Then
                                        cell_revenue.Text = row("Revenue").ToString
                                    End If
                                End If

Open in new window

Hey louise!!!
I'm not much used with VB.Net, but i did an example, take a look on the files...
The solution is very simple in C# as you can see here:
http://www.thescarms.com/dotnet/EventHandler.aspx
Hope it helps.
Boccio
Default.aspx
Default.aspx.designer.vb
Default.aspx.vb

Author

Commented:
Hi Boccio,

Thanks for your code and sorry not to have replied for a while. I created a demo site under IIS using your pages and it works perfectly, however when I paste the exact code into my actual site I get this error:
" 'AddHandler' or 'RemoveHandler' statement event operand must be a dot-qualified expression or a simple name" relating to "AddHandler txtEditRevenueTextChanged" in the page load event. Do you know why that might be?
Thanks again,

Louise

Author

Commented:
Hi again, ignore my last comment - it was down to a typo on my part (revenues not revenue in one place, I think it got in there when I hit ctrl+s which turned out to be just s). Anyway it's working in my site just as it does for the test site, so tomorrow I'm going to see if I can get it to work for multiple text boxes.

Cheers,

Louise

Author

Commented:
Thanks for your help.

Best wishes,
Louise
You are welcome!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial