Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Loop through textbox controls

0 views
Skip to first unread message

David C

unread,
Nov 19, 2009, 12:59:16 PM11/19/09
to
Is there a way to loop through all TextBox controls of a FormView and blank
them out? Thanks.

David


Mr. Arnold

unread,
Nov 19, 2009, 1:14:48 PM11/19/09
to


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/

David C

unread,
Nov 19, 2009, 1:34:58 PM11/19/09
to

"Mr. Arnold" <Arn...@Arnold.com> wrote in message
news:%23W3oDQU...@TK2MSFTNGP06.phx.gbl...

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

unread,
Nov 19, 2009, 2:11:51 PM11/19/09
to

I mislead you. The example I gave you was loop on a Web form not a FormView.


You may still need a loop using the FormView.FindControl.

http://geekswithblogs.net/azamsharp/archive/2006/05/22/79294.aspx

HTH

David C

unread,
Nov 19, 2009, 2:22:01 PM11/19/09
to

"Mr. Arnold" <Arn...@Arnold.com> wrote in message
news:eqy67vUa...@TK2MSFTNGP02.phx.gbl...

Sorry, this does not help me. I know how to loop through a single control
like a listbox. I need to loop through all controls in a FormView. Thanks.

David


Mr. Arnold

unread,
Nov 19, 2009, 3:05:44 PM11/19/09
to

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.


David C

unread,
Nov 19, 2009, 3:15:10 PM11/19/09
to

"Mr. Arnold" <Arn...@Arnold.com> wrote in message
news:u5PCDOV...@TK2MSFTNGP06.phx.gbl...
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

Mr. Arnold

unread,
Nov 19, 2009, 3:25:22 PM11/19/09
to

I am glad it worked out for you.

Mark Rae [MVP]

unread,
Nov 19, 2009, 7:36:55 PM11/19/09
to
"David C" <dlc...@lifetimeinc.com> wrote in message
news:%23RsTzUV...@TK2MSFTNGP04.phx.gbl...

> 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.

That is because when any server-side's control's Visible property is set to
"false", it doesn't actually get rendered in the HTML streamed down to the
client...


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

0 new messages