Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I created the textbox at runtime in Table. how do i get a value from that all textbox.
Here is My Code
VB
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Session("count") = 0
            Dim table As Table = New Table()

            table.ID = "Tab1"
            Page.Form.Controls.Add(table)
            Dim i As Integer
            For i = 0 To 3
                Dim text As TextBox = New TextBox()
                text.ID = "text_" & i

                Page.Form.Controls.Add(New TextBox())
            Next
            Session("count") = Session("count") + 1
        End If
    End Sub


VB
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

Dim ds As DataTable = New DataTable()
        ds.Columns.Add("Textboxid")
        Dim tab As Table = New Table()
        tab.ID = "Table1"
        Page.Form.Controls.Add(tab)
        Dim too As Integer = Session("count")

        For i = 0 To too
            Dim row As TableRow = New TableRow()
            For j = 0 To 3
                Dim cell As TableCell = New TableCell()
                Dim textb As TextBox = New TextBox()
                textb.ID = "text_" & i & "Col" & j
                ds.Rows.Add(textb.ID)
                cell.Controls.Add(textb)
                row.Cells.Add(cell)
            Next

            tab.Rows.Add(row)
        Next
        Session("count") = Session("count") + 1
        Session("dataTable") = ds
    End Sub

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click

-----HERE I WANT CODE WHEN I CLICK THIS BUTTON CONTROL I WANT GET A VALUE FROM DYNAMICALLY CREATED TEXBOX.


 End Sub
Posted
Updated 11-Oct-12 22:50pm
v3
Comments
Peeyush Pachaori 12-Oct-12 4:44am    
Show code please.

On page postback (at example in OnInit event), recreate the same control (with the same ID) and you will be able to retrieve the value as with every other control.

Let's say that you added some control in a panel.Controls.Add(myTextBox). Now add it again on PostBack or CallBack in the OnInit event.

Cheers
 
Share this answer
 
We all tend to think( I used to) that once we dynamically add a control to a page it will automatically be available from then onwards (forgive me if someone never been in this category). This misconception occurs because we look at the normal controls added in the .aspx page page and think once we added they were all automatically available in all the postbacks from then on with data,we didnt need to do a single thing, but what we dont realize is even those controls were recreated and assigned the posted back data during each post back. Even viewstate is sometimes expected to come to the rescue in case of dynamically adding controls :-), but only when we realize whats exactly happening behind the screen(page life cycle) and what viewstate is that we can laugh at what we used think. Refer to this article it can help a lot.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900