|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
ASP.Net 2.0 Validation Groups & IValidator Controls.But has anyone managed to sort out a solution to Validators that directly Implement IValidator (as opposed to inheriting from BaseValidator) error message's not appearing in the ValidationSummary. Starting point for my control was.... http://codeproject.com/aspnet/selfvalidatingtextbox.asp, http://www.codetools.com/aspnet/SelfValidatingTextBox2.asp I've overridden the Validate method and GetValidators on the page to work with validationGroups...the default page ones cast all registered validators to BaseValidator.... But validationSummary doesn't want to play ball..Reflecting the ValidationSummary.GetErrorMessages : Dim collection1 As ValidatorCollection = Me.Page.GetValidators(Me.ValidationGroup) But this isn't firing my custom Page.GetValidators call....which I find odd... My workaround at the moment is to copy the reflected code of ValidationSummary into a customValidationSummary control and alter that so that it grabs the method via reflection... 'Dim collection1 As ValidatorCollection = Me.Page.GetValidators(Me.ValidationGroup) Dim collection1 As ValidatorCollection Dim oMethodInfo As System.Reflection.MethodInfo = Me.Page.GetType.GetMethod("GetValidators") collection1 = oMethodInfo.Invoke(Me.Page, New Object() {Me.ValidationGroup}) Dim collection2 As ValidatorCollection = Me.Page.GetValidators(Me.ValidationGroup) This isn't that elegant so I thought I'd pitch for other suggestions...(apart from, Create a baseValidator inherited validation control...and replace the single self validating control with a textbox and the new validation control.) Cheers.. C. Public Overrides Sub Validate(ByVal validationGroup As String) MyBase.Validate(validationGroup) Dim vals As ValidatorCollection = GetValidators(validationGroup) If String.IsNullOrEmpty(validationGroup) AndAlso Me.Validators.Count = vals.Count Then Me.Validate() Else Dim i As Integer = 0 Do While (i < vals.Count) vals.Item(i).Validate() i += 1 Loop End If End Sub Public Overloads Function GetValidators(ByVal validationgroup As String) As ValidatorCollection Dim coll As New ValidatorCollection For Each obj As Object In Page.Validators Dim val As BaseValidator = TryCast(obj, BaseValidator) If Not val Is Nothing AndAlso String.Compare(val.ValidationGroup, validationgroup, StringComparison.Ordinal) = 0 Then coll.Add(val) Else Dim ival As IValidator = TryCast(obj, IValidator) If Not ival Is Nothing Then 'IValidator dsoesn't expose validation group Dim objtype As Type = obj.GetType() Dim prop As System.Reflection.PropertyInfo = objtype.GetProperty("ValidationGroup", System.Reflection.BindingFlags.Public Or System.Reflection.BindingFlags.Instance) Dim group As String = prop.GetValue(obj, Nothing).ToString() If String.Compare(group, validationgroup, StringComparison.Ordinal) = 0 Then coll.Add(ival) End If End If End If Next Return coll End Function > Implement IValidator (as opposed to inheriting from BaseValidator) error Have you checked that the ValidationGroup of ValidationSummary is matching?> message's not appearing in the ValidationSummary. -- Happy Hacking, Gaurav Vaish | http://www.mastergaurav.com http://www.edujinionline.com http://articles.edujinionline.com/webservices ------------------- Ignore my previous mail...
for all non-BaseValidator derived validators, it adds only if the ValidationGroup of ValidationSummary is string.Empty. Not sure if it has already been reported as a bug or a known-issue or a feature-as-is. -- Show quoteHide quoteHappy Hacking, Gaurav Vaish | http://www.mastergaurav.com http://www.edujinionline.com http://articles.edujinionline.com/webservices ------------------- "Craig" <Cr***@discussions.microsoft.com> wrote in message news:E74E5F13-EBDA-4A64-A8B0-8530FFCD3C68@microsoft.com... > Ok so ASP.Net 2.0 Validation Groups are great and lovely... > > But has anyone managed to sort out a solution to Validators that directly > Implement IValidator (as opposed to inheriting from BaseValidator) error > message's not appearing in the ValidationSummary. > > Starting point for my control was.... > http://codeproject.com/aspnet/selfvalidatingtextbox.asp, > http://www.codetools.com/aspnet/SelfValidatingTextBox2.asp > > I've overridden the Validate method and GetValidators on the page to work > with validationGroups...the default page ones cast all registered > validators > to BaseValidator.... > > But validationSummary doesn't want to play ball..Reflecting the > ValidationSummary.GetErrorMessages : > > Dim collection1 As ValidatorCollection = > Me.Page.GetValidators(Me.ValidationGroup) > > But this isn't firing my custom Page.GetValidators call....which I find > odd... > > My workaround at the moment is to copy the reflected code of > ValidationSummary into a customValidationSummary control and alter that so > that it grabs the method via reflection... > > 'Dim collection1 As ValidatorCollection = > Me.Page.GetValidators(Me.ValidationGroup) > > Dim collection1 As ValidatorCollection > Dim oMethodInfo As System.Reflection.MethodInfo = > Me.Page.GetType.GetMethod("GetValidators") > collection1 = oMethodInfo.Invoke(Me.Page, New Object() > {Me.ValidationGroup}) > > Dim collection2 As ValidatorCollection = > Me.Page.GetValidators(Me.ValidationGroup) > > This isn't that elegant so I thought I'd pitch for other > suggestions...(apart from, Create a baseValidator inherited validation > control...and replace the single self validating control with a textbox > and > the new validation control.) > > Cheers.. > > C. > > > Public Overrides Sub Validate(ByVal validationGroup As String) > MyBase.Validate(validationGroup) > Dim vals As ValidatorCollection = GetValidators(validationGroup) > If String.IsNullOrEmpty(validationGroup) AndAlso > Me.Validators.Count > = vals.Count Then > Me.Validate() > Else > Dim i As Integer = 0 > Do While (i < vals.Count) > vals.Item(i).Validate() > i += 1 > Loop > > End If > > End Sub > > Public Overloads Function GetValidators(ByVal validationgroup As > String) > As ValidatorCollection > Dim coll As New ValidatorCollection > > For Each obj As Object In Page.Validators > Dim val As BaseValidator = TryCast(obj, BaseValidator) > > If Not val Is Nothing AndAlso > String.Compare(val.ValidationGroup, validationgroup, > StringComparison.Ordinal) = 0 Then > coll.Add(val) > Else > > Dim ival As IValidator = TryCast(obj, IValidator) > If Not ival Is Nothing Then > > 'IValidator dsoesn't expose validation group > Dim objtype As Type = obj.GetType() > > Dim prop As System.Reflection.PropertyInfo = > objtype.GetProperty("ValidationGroup", > System.Reflection.BindingFlags.Public > Or System.Reflection.BindingFlags.Instance) > Dim group As String = prop.GetValue(obj, > Nothing).ToString() > > If String.Compare(group, validationgroup, > StringComparison.Ordinal) = 0 Then > coll.Add(ival) > End If > > End If > > > End If > > Next > > Return coll > End Function > Have you found a solution to this problem? I am stuck on exactly this issue and it looks like I have got pretty much the same code.
what I don't understand is why the call to Page.GetValidators still calls the system.web.ui.page method instead of the overridden method in my custom page (which inherits from system.web.ui.page). Any more information would be invaluable.
Using ASP.NET 2.0's ImageMap Control
Treeview Styles Aren't working How to hide the "Edit" link in DetailsView via code? asp.net 2 report problem with 3-tier design White strip showing up between two imagemaps UserControls in a different directory ASP code Controls calling other controls problem - ASP.NET 2.0 Authentication in ASP.NET 2.0 create webcontrols in aspx or aspx.cs |
|||||||||||||||||||||||