• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 419
  • Last Modified:

Display Pop Up Error Box like " Summary Validator "

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
Glass
Asked:
Glass
  • 3
  • 3
1 Solution
 
dfiala13Commented:
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
 
GlassAuthor Commented:
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
 
dfiala13Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
GlassAuthor Commented:
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
 
dfiala13Commented:
>>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
 
GlassAuthor Commented:
Thanxs!  

Glass
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now