How to clear values from dynamically created text boxes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adarshyam
    New Member
    • Oct 2008
    • 31

    How to clear values from dynamically created text boxes

    can anybody tel me how to clear values from dynamically created text boxes?? using a clear button.. this is the code used to create dynamic text boxes.. if i press clear button then it must clear all the values entered in al the textboxes



    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If IsPostBack Then
    Try
    If Not TextBox1.Text Is String.Empty Then
    numofper = TextBox1.Text

    ReDim TxtDyn1(numofpe r)
    ReDim TxtDyn2(numofpe r)
    ReDim TxtDyn3(numofpe r)
    ReDim TxtDyn4(numofpe r)

    Else
    Exit Sub
    End If

    'To create the dynamic text box and add the controls to panel, save them in session state

    For i As Integer = 0 To numofper - 1
    TxtDyn1(i) = New TextBox
    TxtDyn1(i).Enab leViewState = True
    Panel1.Controls .Add(TxtDyn1(i) )
    textdynamic1.Ad d(textdynamic1)
    Next
    For j As Integer = 0 To numofper - 1
    TxtDyn2(j) = New TextBox
    TxtDyn2(j).Enab leViewState = True
    Panel2.Controls .Add(TxtDyn2(j) )
    textdynamic2.Ad d(textdynamic2)
    Next
    For k As Integer = 0 To numofper - 1
    TxtDyn3(k) = New TextBox
    TxtDyn3(k).Enab leViewState = True
    Panel3.Controls .Add(TxtDyn3(k) )
    textdynamic3.Ad d(textdynamic3)
    Next
    For l As Integer = 0 To numofper - 1
    TxtDyn4(l) = New TextBox
    TxtDyn4(l).Enab leViewState = True
    Panel4.Controls .Add(TxtDyn4(l) )
    textdynamic4.Ad d(textdynamic4)
    TxtDyn4(l).Enab led = False
    Next

    Catch ex As Exception
    MsgBox("Enter a valid Number")
    End Try
    End If

    End Sub
  • PRR
    Recognized Expert Contributor
    • Dec 2007
    • 750

    #2
    Loop through all the controls in your panel... then clear them....
    Code:
    TextBox v;
    foreach (Control ctrl in Panel1.Controls)
            {
                if (ctrl is TextBox)
                {
                    v=(TextBox)ctrl;
                    v.Text =string.Empty;
                }
            }
    }

    Comment

    Working...