Get Controls
am 26.01.2008 21:40:19 von 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
RE: Get Controls
am 26.01.2008 22:35:00 von mily242
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
--
Milosz
"shapper" wrote:
> 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
>
Re: Get Controls
am 30.01.2008 12:02:18 von peter.bucher
Hello Miguel
You can also simply implement the IValidator interface,
to your Controls.
(Translated) Example at:
http://translate.google.com/translate?u=http%3A%2F%2Fwww.asp netzone.de%2Fblogs%2Fpeterbucher%2Farchive%2F2007%2F06%2F22% 2Fselfvalidatingcontrol-customcontrol-selber-validieren-lass en.aspx&langpair=de%7Cen&hl=de&ie=UTF-8
--
Gruss, Peter Bucher
Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET
"shapper" schrieb im Newsbeitrag
news:7959cb42-c781-409d-b433-a85e12061f9d@j78g2000hsd.google groups.com...
> 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