[RESOLVED] Enumerating textboxs
Hi All,
I have a number of asp textboxes on my page
I also have a clear button. How can I clear the text in my textbox's without refering to each of them individually.
I thought I might be able to do this, but it doesn't work.
Dim objControl As Control
For Each objControl In Me.Form.Controls
If TypeOf objControl Is TextBox Then
DirectCast(objControl, TextBox).Text = ""
End If
Next
# 1 Re: [RESOLVED] Enumerating textboxs
Try...
Dim objControl As Control
For Each objControl In Me.Form.Controls
If TypeOf objControl Is TextBox Then
CType(objControl, TextBox).Clear()
End If
Next
# 2 Re: [RESOLVED] Enumerating textboxs
Hi,
I've just replaced my line of code with your code:
CType(objControl, TextBox).Clear()
but it errors with Clear is not a member of system.web.ui.controls.textbox.
I thought I might get away with
CType(objControl, TextBox).text = ""
instead of Directcast, but still the same problem.
# 3 Re: [RESOLVED] Enumerating textboxs
Funny, I am not the only one who seems to think that Clear() is. Take a look at this ( http://www.codeproject.com/useritems/SimilarConrtols.asp).
EDIT: Have you tried changing Me.Form.Controls to Me.Controls.
# 4 Re: [RESOLVED] Enumerating textboxs
I did try ME.Controls, and it didn't make any difference.
See what you mean, but it definitely is'nt working for me. I'm using the latest .Net Framework and VS2005.
I cant find which versions the article are using.
# 5 Re: [RESOLVED] Enumerating textboxs
What part of the loop is failing? Can it find the Text for each control and is just failing to set the Text to "", or is it not even finding each Textbox?
# 6 Re: [RESOLVED] Enumerating textboxs
using my code it's not even finding the textbox.
I thought I'd do a variant of the code you pointed me to, as there's a subtle difference in that it uses the Gettype method so I used the code below, but it still doesn't even find a textbox.
Dim textBoxType As Type = txtAgent.GetType
For Each objControl As Control In Me.Controls
If objControl.GetType() Is textBoxType Then
'If TypeOf objControl Is TextBox Then
CType(objControl, TextBox).Text = ""
End If
Next
If I change my For Each statement to
For Each objControl As Control In Me.Form.Controls
Then it hit's the Ctype line once only. there are a number of textboxes on the form.
# 7 Re: [RESOLVED] Enumerating textboxs
If worst comes to worse, you can use OnClientClick to tell JavaScript to reset the textboxes. Are these textboxes in a form or outside a form? Are some inside and some outside?
# 8 Re: [RESOLVED] Enumerating textboxs
Thanks for your efforts.
I've had a reply that works from another site.
Private Sub ClearMe(ByVal ParentControl As Control)
For Each c As Control In ParentControl.Controls
If TypeOf c Is TextBox Then
DirectCast(c, TextBox).Text = ""
End If
' recursively call this function for each child control
For Each child As Control In c.Controls
Call ClearMe(child)
Next child
Next c
End Sub
# 9 Re: [RESOLVED] Enumerating textboxs
I've had a reply that works from another site.
Glad you solved it. Sorry I couldn't be of more help.