Monday, March 26, 2012

loop through controls to get text values not working

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...
Thanks in advance,
StevenI would try to cast the ctl object into a textbox at the point where
you are trying to access the .Text property.
"Steven K" <skaper@.troop.com> wrote in message news:<O2$HDPrFEHA.744@.TK2MSFTNGP09.phx.gbl>.
.
> Hello,
> I using the following to loop through my controls to get the text value fo
r
> 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 commen
ts
> ("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...
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 collectio
n 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 TextB
ox 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 String
For Each ctl As Control In Page.Controls
If TypeOf(ctl) Is TextBox Then
TextBox myTextBox = (TextBox) ctl
strTextBox = myTextBox.Text
response.write(strTextBox & " hello<br>")
End If
Next
End Sub
HTH
Cheers
Ashish Bhonkiya
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

0 comments:

Post a Comment