Solved

Display Pop Up Error Box like " Summary Validator "

Posted on 2004-04-25
6
408 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

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.

Join & Write a Comment

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 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