FindControl only for ASP.NET ?

  • Thread starter Thread starter Tee
  • Start date Start date
T

Tee

Hi,

I have 3 Button, Button1, Button2, and Button3.

I have a variable i which will have the value of 1, 2, or 3.

then I need to code like this

(Button & i).visible = false

Anyone know how to do it ?

in ASP.NET, I can use findcontrol("button" & i).visible = false, but in
VB.NET, is there a function like findcontrol ?

Please help.



thanks,
Tee
 
Hi Tee,

For that you can make an array of your (existing) buttons

Dim Buttons As Button() = New Button() {Button1, Button2}

Buttons(1).visible = false

I hope this helps?

Cor
 
Hi Cor ! Thanks for your always fast reply.


This works and helped me. But I wonder if there is any function that
similiar to FindControl ?


Thanks.
Tee
 
Its not really diffucult...


public function FindControl(ctlName as String) as Control

Dim ctl as Control

for each ctl in me.Controls
if ctl.Name = ctlName then
return ctl
end if
next

end function
 
Cool ! Thanks A lot CJ ...


CJ Taylor said:
Its not really diffucult...


public function FindControl(ctlName as String) as Control

Dim ctl as Control

for each ctl in me.Controls
if ctl.Name = ctlName then
return ctl
end if
next

end function
 
Hi CJ,

I cannot use your FindControl function to target my UserControl.

error message :

An unhandled exception of type 'System.NullReferenceException' occurred in
CT.exe
Additional information: Object reference not set to an instance of an
object.


is it my usercontrol has problem ? or this code can't be used on UserControl
?


Thanks,
Tee
 
* "Tee said:
I have 3 Button, Button1, Button2, and Button3.

I have a variable i which will have the value of 1, 2, or 3.

then I need to code like this

(Button & i).visible = false

Anyone know how to do it ?

in ASP.NET, I can use findcontrol("button" & i).visible = false, but in
VB.NET, is there a function like findcontrol ?

\\\
Private Function FindControl( _
ByVal ControlName As String, _
ByVal CurrentControl As Control _
) As Control
Dim ctr As Control
For Each ctr In CurrentControl.Controls
If ctr.Name = ControlName Then
Return ctr
Else
ctr = FindControl(ControlName, ctr)
If Not ctr Is Nothing Then
Return ctr
End If
End If
Next ctr
End Function
///

Usage:

\\\
DirectCast(FindControl("btnBla", Me), Button).Enabled = False
///

Notice that the procedure listed above is "slow", if you have to access a
lot of controls by name very often, you should store references to them in a
'Hashtable' object. You can use the name of the control as key:

\\\
Private m_Controls As New Hashtable()
///

Adding a control:

\\\
m_Controls.Add(DynamicPictureBox.Name, DynamicPictureBox)
///

Looking for a control:

\\\
Dim p As PictureBox = DirectCast(m_Controls.Item("PictureBox1"), PictureBox)
///

Removing a control:

\\\
m_ht.Remove("PictureBox1")
///

Sometimes it's even better to add the control to an array. This will allow
fast and easy index-based access to the control references.
 
Back
Top