Get Controls

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
Shapper [ Sa, 26 Januar 2008 21:40 ] [ ID #1916776 ]

RE: Get Controls

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
>
mily242 [ Sa, 26 Januar 2008 22:35 ] [ ID #1916780 ]

Re: Get Controls

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" <mdmoura [at] gmail.com> schrieb im Newsbeitrag
news:7959cb42-c781-409d-b433-a85e12061f9d [at] 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
peter.bucher [ Mi, 30 Januar 2008 12:02 ] [ ID #1919884 ]
Microsoft » microsoft.public.dotnet.framework.aspnet » Get Controls

Vorheriges Thema: Trotz iFrame läd sich im IE die ganze Seite neu??!?
Nächstes Thema: Create image with path read from query