Get Controls

S

shapper

Hello,

I have 2 custom controls A and B which inherit from another custom
control named Child.

A and B have a method named Validate.

Now I added about 10 controls of type A and B to an Asp.Net Panel.

I need to loop through each A/B control inside Asp.Net Panel and run
its Validate method.

How can I do this?

Thanks,

Miguel
 
M

Milosz Skalecki [MCAD]

Hi Shapper,

This is a classic example of polymorphysm:
Base class must have virtual (overridable in vb.net) method Validate()
Descandant classes override Validate method. You call BaseClass.Validate(),
but each instance provides its own implementation.

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

For Each ctrl As Control In panel.Controls

If TypeOf ctrl Is BaseControl Then
CType(ctrl, BaseControl).Validate()
End If
Next

End Sub

End Class

Class ControlA
Inherits BaseControl

Public Overrides Sub Validate()
MyBase.Validate()
Text = "Control A "
End Sub
End Class

Class ControlB
Inherits BaseControl

Public Overrides Sub Validate()
MyBase.Validate()
Text = "Control B "
End Sub
End Class

Class BaseControl
Inherits TextBox

Public Overridable Sub Validate()

End Sub
End Class
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top