Looping through textboxes - error in my code?

N

Nick

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
 
C

Crosta

In 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

?
 
N

Nick

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
 
T

Tee

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
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top