Connecting Tech Pros Worldwide Forums | Help | Site Map

How to clear values from dynamically created text boxes

Newbie
 
Join Date: Oct 2008
Posts: 31
#1: Oct 13 '08
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(numofper)
ReDim TxtDyn2(numofper)
ReDim TxtDyn3(numofper)
ReDim TxtDyn4(numofper)

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).EnableViewState = True
Panel1.Controls.Add(TxtDyn1(i))
textdynamic1.Add(textdynamic1)
Next
For j As Integer = 0 To numofper - 1
TxtDyn2(j) = New TextBox
TxtDyn2(j).EnableViewState = True
Panel2.Controls.Add(TxtDyn2(j))
textdynamic2.Add(textdynamic2)
Next
For k As Integer = 0 To numofper - 1
TxtDyn3(k) = New TextBox
TxtDyn3(k).EnableViewState = True
Panel3.Controls.Add(TxtDyn3(k))
textdynamic3.Add(textdynamic3)
Next
For l As Integer = 0 To numofper - 1
TxtDyn4(l) = New TextBox
TxtDyn4(l).EnableViewState = True
Panel4.Controls.Add(TxtDyn4(l))
textdynamic4.Add(textdynamic4)
TxtDyn4(l).Enabled = False
Next

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

End Sub

PRR PRR is offline
Moderator
 
Join Date: Dec 2007
Location: India
Posts: 701
#2: Oct 14 '08

re: How to clear values from dynamically created text boxes


Loop through all the controls in your panel... then clear them....
Expand|Select|Wrap|Line Numbers
  1. TextBox v;
  2. foreach (Control ctrl in Panel1.Controls)
  3.         {
  4.             if (ctrl is TextBox)
  5.             {
  6.                 v=(TextBox)ctrl;
  7.                 v.Text =string.Empty;
  8.             }
  9.         }
  10. }
  11.  
Reply


Similar .NET Framework bytes