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
327 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 - SQL Timeout 9 34
bound data table problem 2 33
VS 2017 18 49
Code works but breaks when I add one section 4 20
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

829 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