Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
333 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
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 1000 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

886 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