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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top