Click to See Complete Forum and Search --> : Enable buttons


Cristiane
July 17th, 2003, 04:41 PM
Hi...

I ask your help for the following problem....
I'm trying to enable/disable buttons in my Page_Load event. I wanna loop among my controls and find the buttons... and if the text property has an x value, enable the button, otherwise, disable the button. That's the code I'm using.... It doesn't work. I have 4 buttons in my page and they aren't any of this "controls" because they don't "enter" in the loop....
Can somebody help me, please? I have no idea what is happening....
Thanks
Cristiane

For Each myControl In Me.Controls
If TypeOf myControl Is Button Then
myBut = CType(myControl, Button)
For i = 0 To dsScreen.Tables(0).Rows.Count - 1
If myBut.Text = dsScreen.Tables(0).Rows(i).Item(1) Then
myBut.Enabled = True
Else
myBut.Enabled = False
End If
Next
End If
Next
:confused:

Rohit Kukreti
July 18th, 2003, 08:01 AM
Hi,

you are using vb.net as the language. So, unlike c# you'll have to declare the myControl, myBut, etc. before using the the objects

try the codew below its ur same code with just the definition added

Dim myControl As Control
Dim myBut As Button
For Each myControl In Me.Controls
If TypeOf myControl Is Button Then
myBut = CType(myControl, Button)
For i = 0 To dsScreen.Tables(0).Rows.Count - 1
If myBut.Text = dsScreen.Tables(0).Rows(i).Item(1) Then
myBut.Enabled = True
Else
myBut.Enabled = False
End If
Next
End If
Next

Cristiane
July 18th, 2003, 08:24 AM
Hi,

Thank you Rohit but, in spite of not writting in this post, I've already declared myBut and myControl in my code...
I think my problem is in fact that the Me.Controls doesn't identify my controls....
If you have any other suggestion, really thanks
Cristiane
:confused:

Rohit Kukreti
July 18th, 2003, 08:40 AM
Hi Cristiane,
I tested the code below and it recognises the button. I feel this should help.

Dim obj As Object
Dim i As Integer
For Each obj In Controls(1).Controls
If TypeOf obj Is Button Then
If obj.GetType().ToString().IndexOf("Button") > 0 Then
'myBut = CType(myControl, Button)
For i = 0 To 1
If obj.Text = "Button" Then
obj.Enabled = True
Else
obj.Enabled = False
End If
Next
End If
End If
Next

Post if any thing required

hope it helps
--
Rohit

Cristiane
July 18th, 2003, 11:02 AM
Hi, Rohit

Thanks!!!! You really solve my problem!!!!
Cristiane:D