Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CustomValidator Not working with ValidationSummary 1

Status
Not open for further replies.

ksbigfoot

Programmer
Apr 15, 2002
856
CA
I am using VS 2005.
I have a FileUpload field, textbox, and a gridview.
I have a ValidationSummary.
I have two RequiredFieldValidators for the FileUpload and textbox, they work properly on the client side if the fields are not populated.
I created a CustomValidator for the Gridview, I get the red * to show up beside the Gridview, but I am not sure how to get the ValidationSummary to pop up showing that at least one checkbox needs to be checked in the Gridview.

Here is my code
Code:
<asp:GridView ID="GridView1" ...

<asp:CustomValidator ID="CVGridView1" 
             OnServerValidate="CVGridView1_ServerValidate"
                        EnableClientScript="false"
                        SetFocusOnError="true"
                        Text="*" 
                        runat="server"
                        ErrorMessage="Distribution Type must be selected" />

<asp:ValidationSummary ID="ValidationSummary1"
ShowMessageBox="true"
ShowSummary="false"
HeaderText="You must enter a value in the following fields:"
EnableClientScript="true"
runat="server"/>

Code behind:
Code:
    Public Sub CVGridView1_ServerValidate(ByVal source As Object, _
        ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
        'Loop through grid looking for a checked checkbox
        Dim bFlag As Boolean = False

        For i As Integer = 0 To GridView1.Rows.Count - 1

            Dim chk As CheckBox = CType(GridView1.Rows(i).Cells(0).FindControl("chkSelect"), CheckBox)

            If chk IsNot Nothing AndAlso chk.Checked Then
                bFlag = True
            End If
        Next

        args.IsValid = bFlag

    End Sub

My CVGridView1_ServerValidate sets args.IsValid to False if no checkboxes are checked in my Gridview, so I know it is processing the value correctly if no checkboxes are selected.

I am not sure how to display a message in the ValidationSummary if results are processed on server side?
Is there a way on client side to check to see if at least one checkbox has been selected?
 
It's a combination of these two properties that are causing you the problem:
Code:
ShowMessageBox="true" ShowSummary="false"
As far as I know, by default, custom errors are not added to the message box that is shown. If you reverse the values so that you have:
Code:
ShowMessageBox="false" ShowSummary="true"
Then it should display your custom message in the validation summary box (proving that it is just a "feature" of the ValidationSummary control).

I'd just use the summary box itself rather than a popup, but there may be workarounds if this is not what you want to do.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
I wanted to have a pop-up as I wasn't sure how to set focus on the screen to where the messages are being displayed.
I realized that whenever there is an error, I am also losing my FileUpload content. I am wondering if there is a way I can check ClientSide to see if they checked off at least one checkbox?
 
I wanted to have a pop-up as I wasn't sure how to set focus on the screen to where the messages are being displayed.
You can use the Focus method of the control.

I realized that whenever there is an error, I am also losing my FileUpload content.
Any postback will remove the selected file - this is part of the security model.

I am wondering if there is a way I can check ClientSide to see if they checked off at least one checkbox?
Yes, you can write a javascript function to loop through each control.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
Thanks and star to you ca8msm.
I couldn't get my CustomValidator to work on the client side, so I wrote a javascript function. I now get two pop-up boxes which isn't ideal, but will do for now.

I changed my button code to
Code:
<asp:Button ID="btnAssign" OnClick="btnAssign_Click" OnClientClick="confirmMsg(this.form)" runat="server" Text="Assign Distribution Types" Width="184px" /><br />
My confirmMsg will display a pop-up if at least one checkbox isn't checked off. The btnAssign_Click handles the processes if everything is OK.

My ValidationSummary only verifies the FileUpload and TextBox fields. Would of been ideal that it also handled the checkboxes within my Gridview.

Thanks again for answering my questions.
ksbigfoot

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top