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
324 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now