Loop through textbox controls

  • Thread starter Thread starter David C
  • Start date Start date
D

David C

Is there a way to loop through all TextBox controls of a FormView and blank
them out? Thanks.

David
 
Mr. Arnold said:
Yes, you can loop through all the controls on the Web from, check what
type of UI control object it is, and address the properties of the UI
control object.

ctrl.Text = "" if the control has a Text property, as an example.

http://dotnetcoderoom.wordpress.com/2009/03/07/traverse-loop-through-all-form-controls-in-aspnet/

I tried the link (it was in C# and I use VB) and created the sub below, but
it does not seem to work as after running it the controls still have data.
Can you see any problems? Thanks.

David

Private Sub ClearForm()
Dim fv As FormView = fvPeople
Dim tb As TextBox
For Each ctrl As System.Web.UI.Control In fvPeople.Controls
If TypeOf ctrl Is TextBox Then
tb = ctrl
tb.Text = ""
End If
Next
End Sub
 
Mr. Arnold said:
A FormView is a databound control based on rows like a table, right?

If it were me, I would find a way to loop through the table rows, find the
control, an object, I was looking for on the row using FV.FindControl(),
and address the object's properties. Having not worked with a FV, I would
suspect that is the way it must be done.

The example provided shows you how to find the control on the FV. It's up
to you to think outside of the box and find a way to implement the
solution to address the problem.
Actually, a FormView is a single-row control and not a grid/table.

I got my code to work ok now. I was setting the FormView control to
visible=False and it did not work. I changed it to a CSSClass that was
hidden and it worked. Below is my finished code, if interested.

Private Sub ClearForm()
Dim tb As TextBox
Dim ctrl As Control
For Each ctrl In fvPeople.Controls
If TypeOf ctrl Is TextBox Then
tb = ctrl
tb.Text = ""
End If
Next

End Sub
 
Back
Top