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
329 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
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)…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 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