Solved

Can an Asp.Net Validation Summary control be used to validate a Group of controls instead of all the controls on a page ?

Posted on 2004-03-27
11
326 Views
Last Modified: 2012-06-21
Simple question:

I'd like to provide a validation summary for three groups of controls on a page, one summary for each group. Is it possible to have more than one validation summary on a page, can each one summarise a subset of all the validation controls and, if this is possible, how do i hook this up ?

Many thanks in advance for any assistance.
0
Comment
Question by:judefisher
  • 3
  • 3
  • 2
  • +1
11 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 10695616
first, put a label on your page with id "lblMsg". This message will display an error message. Then add the following function to your code:

Sub CheckFields()
        Dim myForm As Control = Page.FindControl("Form1")
        Dim myMsg As Label
        myMsg = FindControl("lblMsg")
        Dim ctl As Control
        Dim myText As String
        Dim blankCount, iIndex As Integer
        blankCount = 0
        For Each ctl In myForm.Controls
            If ctl.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox") Then
                myText = CType(ctl, TextBox).Text
                If myText = "" Then
                    blankCount += 1
                End If
            ElseIf ctl.GetType().ToString().Equals("System.Web.UI.WebControls.DropDownList") Then
                iIndex = CType(ctl, DropDownList).SelectedIndex
                If iIndex = 0 Then
                    blankCount += 1
                End If
            End If
        Next ctl
        If blankCount = 0 Then
            myMsg.ForeColor = System.Drawing.Color.Red
        Else
            myMsg.Text = "No blanks allowed!"
        End If
    End Sub

This procedure will validate TextBox and DropDownList controls.  
Call the procedure from your Page_Load:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If IsPostBack Then
            CheckFields()
        End If
    End Sub
0
 
LVL 35

Expert Comment

by:YZlat
ID: 10695642
oops, made a mistake. Corrected code:

Sub CheckFields()
        Dim myForm As Control = Page.FindControl("Form1")
        Dim myMsg As Label
        myMsg = FindControl("lblMsg")
        Dim ctl As Control
        Dim myText As String
        Dim blankCount, iIndex As Integer
        blankCount = 0
        For Each ctl In myForm.Controls
            If ctl.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox") Then
                myText = CType(ctl, TextBox).Text
                If myText = "" Then
                    blankCount += 1
                End If
            ElseIf ctl.GetType().ToString().Equals("System.Web.UI.WebControls.DropDownList") Then
                iIndex = CType(ctl, DropDownList).SelectedIndex
                If iIndex = 0 Then
                    blankCount += 1
                End If
            End If
        Next ctl
        If blankCount > 0 Then
            myMsg.ForeColor = System.Drawing.Color.Red
            myMsg.Text = "No blanks allowed!"
        End If
    End Sub
0
 

Author Comment

by:judefisher
ID: 10695673
But isn't that validating all the controls, just like the summary component ?
I need to validate three seperate groups of controls at the same time and display seperate summaries, and I'm dealing with regular expression validations as well as required field validators.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 4

Expert Comment

by:locitt
ID: 10696739
i think you need to make a relation between your required field validators and a validator. Currently, the validation summary displays all the errors in the page. So i think you make your own validation summary control, validators and modify the webuivalidation.js script. lots of things to do...

locitt.
0
 
LVL 35

Expert Comment

by:YZlat
ID: 10696791
if you want a separate message for each control, then just add labels next to each control.
0
 

Author Comment

by:judefisher
ID: 10697942
If I wanted a seperate message for each control, I could just use the built in validators. My problem is that I have 16 controls on a form, each with validation (some with validation of more than one kind. These are client requirements so I can't change the number of validations. I can't fit all of this into 800x600 with validation messages next to each control and I haven't got enough space on the form for a summary if the user breaks more than half the validation rules. I guess my options are either to follow locitt's advice and create my own component or to use the msgbox option for the summary, although I don't like this since I don't think it's easy for users to remember a long list of validation constraints from an alert.
0
 
LVL 4

Accepted Solution

by:
locitt earned 250 total points
ID: 10697974
for each validation, just a "!" mark to indicate required field or sth or change the background color of the control that requires to put some value. So in the summary you can only show a short message ...

That's the way i did with my custom validators and custom  validation summary.

locitt.
0
 

Author Comment

by:judefisher
ID: 10697981
Thanks, locitt - I'm going to leave this open a little longer in case someone has some code for creating sub-summaries but you're up for the points as we stand at the moment.
0
 
LVL 1

Expert Comment

by:tupalee
ID: 14583576
I know this is closed - but I found this - the last example looks to be what you were looking for
http://samples.gotdotnet.com/quickstart/aspplus/doc/webvalidation.aspx
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question