loop through controls to get text values not working

S

Steven K

Hello,

I using the following to loop through my controls to get the text value for
each text box. However, I am getting the following error. Even if I
comment out the code "strTextBox = ctl.Text", I still don't get any comments
("hello").:

'Text' is not a member of 'System.Web.UI.Control'.

Sub LoopingControls02_Click (Sender As Object, Args As EventArgs)
Dim strTextBox as String
For Each ctl As Control In Page.Controls
If TypeOf(ctl) Is TextBox Then
strTextBox = ctl.Text
response.write(strTextBox & " hello<br>")
End If
Next
End Sub

Any help would be appreciated with this...
 
G

Guest

Steven, you need to cast the control to a textbox, because the generic control doesn't have a Text property, e.g

strTextBox = ctype(ctl, TextBox).Tex

Bil

----- Steven K wrote: ----

Hello

I using the following to loop through my controls to get the text value fo
each text box. However, I am getting the following error. Even if
comment out the code "strTextBox = ctl.Text", I still don't get any comment
("hello").

'Text' is not a member of 'System.Web.UI.Control'

Sub LoopingControls02_Click (Sender As Object, Args As EventArgs
Dim strTextBox as Strin
For Each ctl As Control In Page.Control
If TypeOf(ctl) Is TextBox The
strTextBox = ctl.Tex
response.write(strTextBox & " hello<br>"
End I
Nex
End Su

Any help would be appreciated with this..
 
J

Joey Powell

I would try to cast the ctl object into a textbox at the point where
you are trying to access the .Text property.
 
G

Guest

Please Try to Modify the code as follows and it should work
here when you are using for each loop you are getting the controls collection in the pages in to ctl which is of type Control and hence you are getting the error. So what you need to do is after checking the ctl is of type TextBox you need to assign it to a TextBox type control typecasting it as shown in the code below

Sub LoopingControls02_Click (Sender As Object, Args As EventArgs
Dim strTextBox as Strin
For Each ctl As Control In Page.Control
If TypeOf(ctl) Is TextBox The
TextBox myTextBox = (TextBox) ct
strTextBox = myTextBox.Tex
response.write(strTextBox & " hello<br>"
End I
Nex
End Su


HT
Cheer
Ashish Bhonkiya
 
J

Jeff Carver

Steven,

You need to cast the Control object as a TextBox object. You can do
that in two steps, like this:

Dim tbx As TextBox = CType(ctl, TextBox)
strTextBox = tbx.Text

or in a single step like this:

strTextBox = CType(ctl, TextBox).Text

Also, I assume that your code is drilling down through the multiple
levels of control collections to allow for the fact that textboxes are
not in the control collection that is immediately subordinate to the
Page object. For example, this allows for textbox controls that
belong to the HtmlForm control in the page's top-level control
collection and also textbox controls on a panel subordinate to the
HtmlForm control:

For Each ctlLvl0 As Control In Page.Controls
If TypeOf ctlLvl0 Is HtmlForm Then
For Each ctlLvl1 As Control In ctlLvl0.Controls
If TypeOf ctlLvl1 Is TextBox Then
strTextBox = CType(ctlLvl1, TextBox).Text
Response.Write(strTextBox & " hello<br>")
ElseIf TypeOf ctlLvl1 Is Panel Then
For Each ctlLvl2 As Control In ctlLvl1.Controls
If TypeOf ctlLvl2 Is TextBox Then
strTextBox = CType(ctlLvl2, TextBox).Text
Response.Write(strTextBox & " hello<br>")
End If
Next ctlLvl2
End If
Next ctlLvl1
End If
Next ctlLvl0

If you had subpanels (panels subordinate to the panels on the
HtmlForm), then of course you'd need to add code to the above to
process yet another level ("For Each ctlLvl3 As Control In
ctlLvl2.Controls...").

Hope this helps,
Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top