Solved

Display Pop Up Error Box like " Summary Validator "

Posted on 2004-04-25
6
412 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to alternate between 2 messages until page closes in Classic ASP 8 41
Help with query 3 37
Html split(text) Part2 6 23
ASP Error Handler 5 20
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 informatio…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…

733 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