Friday, March 16, 2012

Looping through textboxes - error in my code?

Hi - I'm trying to loop through the textboxes on a page to unlock them for
editing. The Enabled property is set to false, and the following code is on
an 'Edit' button on the form. Can anyone tell me why it doesn't work?

Thanks

Nick.

Code:
Dim frmCtrl As Control

For Each frmCtrl In Me.Controls

If TypeOf frmCtrl Is System.Web.UI.WebControls.TextBox Then

Dim textbox As System.Web.UI.WebControls.TextBox = frmCtrl

textbox.Enabled = True

End If

NextIn data Thu, 1 Jul 2004 16:39:38 +0100, Nick ha scritto:

> Hi - I'm trying to loop through the textboxes on a page to unlock them for
> editing. The Enabled property is set to false, and the following code is on
> an 'Edit' button on the form. Can anyone tell me why it doesn't work?
> Thanks
> Nick.
>
> Code:
> Dim frmCtrl As Control
> For Each frmCtrl In Me.Controls
> If TypeOf frmCtrl Is System.Web.UI.WebControls.TextBox Then
> Dim textbox As System.Web.UI.WebControls.TextBox = frmCtrl
> textbox.Enabled = True
> End If
> Next

Why instead of this:

Dim textbox As System.Web.UI.WebControls.TextBox = frmCtrl
textbox.Enabled = True

you don't use this:

DirectCast(frmCtrl, System.Web.UI.WebControls.TextBox).Enabled = True

?
Hi - I tried changing the code - but same result. Code runs fine with no
errors, but doesn't enable the textboxes.
Does the postback change them back to enabled=false as that is the default
setting in the IDE??

Nick

"Crosta" <Crosta@.Crosta.Crosta> wrote in message
news:whs2vuwk073s.50803p8cbilc$.dlg@.40tude.net...
> In data Thu, 1 Jul 2004 16:39:38 +0100, Nick ha scritto:
>
> Why instead of this:
> Dim textbox As System.Web.UI.WebControls.TextBox = frmCtrl
> textbox.Enabled = True
> you don't use this:
> DirectCast(frmCtrl, System.Web.UI.WebControls.TextBox).Enabled = True
> ?
I think I knew what you need.
I was having the same problem last time, for each control loop work in
windows form but not web form. In web forms, you need to use a recursive
function to search thru all the levels. I'm not very sure how to explain it,
I will post a code that hope can help you here.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

EnableTextboxes(Controls)

End Sub

Private Sub EnableTextboxes(ByVal col As ControlCollection)

If (col Is Nothing) Then

Return

End If

Dim c As Control

For Each c In col

If TypeOf c Is TextBox Then

CType(c, TextBox).Enabled = True

End If

ChangeTextboxes(c.Controls)

Next

End Sub

With this recursive function, it will search thru all levels of object, eg:
the texbox is inside a panel, a panel is inside a form
if you don't use recursive function, you only get those textboxes in the
form but not the panel.

HTH,
Tee

"Nick" <Nick@.NTWorks.no.spam.fsnet.co.uk> wrote in message
news:egrCis4XEHA.1152@.TK2MSFTNGP09.phx.gbl...
> Hi - I tried changing the code - but same result. Code runs fine with no
> errors, but doesn't enable the textboxes.
> Does the postback change them back to enabled=false as that is the default
> setting in the IDE??
> Nick
> "Crosta" <Crosta@.Crosta.Crosta> wrote in message
> news:whs2vuwk073s.50803p8cbilc$.dlg@.40tude.net...
> > In data Thu, 1 Jul 2004 16:39:38 +0100, Nick ha scritto:
> > Why instead of this:
> > Dim textbox As System.Web.UI.WebControls.TextBox = frmCtrl
> > textbox.Enabled = True
> > you don't use this:
> > DirectCast(frmCtrl, System.Web.UI.WebControls.TextBox).Enabled = True
> > ?
Thanks - that was exactly what I was looking for!

Nick

"Tee" <thy@.streamyx.com> wrote in message
news:%23G8kYp5XEHA.2344@.TK2MSFTNGP11.phx.gbl...
> I think I knew what you need.
> I was having the same problem last time, for each control loop work in
> windows form but not web form. In web forms, you need to use a recursive
> function to search thru all the levels. I'm not very sure how to explain
it,
> I will post a code that hope can help you here.
>
> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button1.Click
> EnableTextboxes(Controls)
> End Sub
>
> Private Sub EnableTextboxes(ByVal col As ControlCollection)
> If (col Is Nothing) Then
> Return
> End If
> Dim c As Control
> For Each c In col
> If TypeOf c Is TextBox Then
> CType(c, TextBox).Enabled = True
> End If
> ChangeTextboxes(c.Controls)
> Next
> End Sub
>
> With this recursive function, it will search thru all levels of object,
eg:
> the texbox is inside a panel, a panel is inside a form
> if you don't use recursive function, you only get those textboxes in the
> form but not the panel.
>
> HTH,
> Tee

0 comments:

Post a Comment