Solved

Display Pop Up Error Box like " Summary Validator "

Posted on 2004-04-25
6
410 Views
Last Modified: 2008-01-16
VB.NET, ASP.NET, IBUYSPY Template, SQL Server, Windows 2000 Server OS

Have created an asp page that  has two text fields  and two drop down boxes with required validators.  Also, implemented the summary validator with the show message box.  

Trying to find a required validator for a checkbox list.  If it exists, need a lead to implement.  If not, then will determine through the checklist box count whether at least one item has been selected.  If none have been selected would like to do one of the following in order of preference:

1) Tie into summary valiadator so that if no checkboxes have been selected then the message box will display.  Don't think that this is possible.

2) Show a message box that is like the one used by the summary validator but have been unsuccessful....

Thanxs,
Glass
0
Comment
Question by:Glass
  • 3
  • 3
6 Comments
 
LVL 12

Expert Comment

by:dfiala13
ID: 10915637
There is no required validator for the Checkbox list.  You will need to implement a custom validator, but you can include it in the validation summary.

Here's code that will add the necessary client script to the page...


    Private Sub AddValScript(ByVal cbl As CheckBoxList)

        Dim s As String
        Dim i As Integer

        s = "<script language='javascript'>"
        s += "function ValCheckBoxList(src, args){"
        s += "bChecked = false;"

        For i = 0 To cbl.Items.Count - 1
            s += "if(document.all[" & Chr(34) & cbl.ID & "_" & i & Chr(34) & "].checked)  bChecked = true;"
        Next
        s += " args.IsValid = bChecked;"

        s += "}"
        s += "</script>"

        RegisterClientScriptBlock("chkval", s)
    End Sub

Call it after you have filled or bound the CheckBoxList control with all the items in the list.

Add a custom validator to the page
Set the ClientValidationFunction on the validator to ValCheckBoxList

The one tricky part is setting the control to validate.  You can't set it to the CheckBoxList control, but you need to set it to a control that a) can be validated and b) will have data entered (so the validation script will be called).

The validating another control technique is a bit klutzy, but otherwise you'll have to dig a little more deeply into how ASP.NET does client-side validation and hook it in yourself.  This lets you use the existing mechanism.
0
 

Author Comment

by:Glass
ID: 10918012
Dumb question heading your way.....

I see the javascript but can this be done with VB as a server script?  It is my intention to perform the check once the user has submitted the form for processing.  

Glass
0
 
LVL 12

Expert Comment

by:dfiala13
ID: 10918568
Of course, fairly trivial on the server side since you can access the objects (I like to do it on the client side to save the trip to the server).

All you need to do is implement the SercerValidate event for the Custom Validator.  Same caveat applies, the CustomValidator must actually "validate"  a control that a) can be validated and b) will have data entered (so the validation function will be called).

    Private Sub CustomValidator1_ServerValidate(ByVal source As System.Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
        Dim i As Integer
        Dim bChecked As Boolean = False
        For i = 0 To CheckBoxList1.Items.Count - 1
            If CheckBoxList1.Items(i).Selected = True Then
                bChecked = True
                Exit For
            End If
        Next
        args.IsValid = bChecked
    End Sub
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:Glass
ID: 10920979
As for as the caveat that you mention regarding 'can be validated', can a customValidator be used on a checkbox list?  I ask since couldn't apply a required field validator like in my drop down boxes.  

Appreciate the help,
Glass
0
 
LVL 12

Accepted Solution

by:
dfiala13 earned 500 total points
ID: 10921107
>>can a customValidator be used on a checkbox list?
Yes, but not directly. You need to get around the fact that validators only allow validation on certain types of controls (which is goofy in this insatnce).  As I mentioned in my original post, this is easily done, you just need to set the ControlToValidate on the custom validator to a TextBox or other control type that: a) can be validated and b) will have data entered.  The CustomValidator will not validate this dummy control -- as you'll notice by the code -- it will actually validate the CheckBoxList . This technique just allows you to easily hook into the .NET validation handling.

0
 

Author Comment

by:Glass
ID: 10924833
Thanxs!  

Glass
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Classic ASP - cannot get to correct path 17 73
SP to delete duplicates 15 70
Select distinct 25 101
Writing lines of code from a form output in script within a results page 2 19
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

810 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