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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CustomValidator Problem

Status
Not open for further replies.

Peppi

Programmer
Apr 9, 2001
205
0
0
CA
Hi,

I have a CustomValidator on my page. The validation is performed server-side. I have Save and Save & Close buttons which cause validation to fire. When I click the Save & Close button, everything works as expected and the error message shows if validation fails. However, no error message shows if I click on the Save button. Really strange.

Here's the relevant markup:

Code:
<td>
                <asp:TextBox ID="tbBackground" runat="server" CssClass="textfield" Width="100px" MaxLength="20" AutoPostBack="True"></asp:TextBox>
            </td>
<td>
                <asp:CustomValidator ID="cvBackground" runat="server" CssClass="rdnwarning" ErrorMessage="Please enter a valid hexadecimal color value (e.g. #0088DD) or valid standard color value (e.g. red)."
                    Display="Dynamic" ControlToValidate="tbBackground"></asp:CustomValidator>     </td>
...
<td><span id="saveChanges" runat="server">
            <asp:LinkButton ID="cmdSave" runat="server" CommandName="Save" OnCommand="Save">Save</asp:LinkButton>
            <span class="bodyseparator">|</span> </span></td>           
<td><span id="saveClose" runat="server">
            <asp:LinkButton ID="cmdSaveClose" runat="server" CommandName="SaveClose" OnCommand="Save">Save & Close</asp:LinkButton>
            <span class="bodyseparator">|</span> </span></td>

The validation code is being called when either button is clicked, and the validation fails, just no error message displaying when Save is clicked. Does anyone know why this might be?

Thx.
 
Is this 2.0? Do you have the validation group set the same for all controls?
 
Yes, 2.0. I don't have the validation group set for any of the controls. They are all empty.
 
Code:
Private Sub cvBackground_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles cvBackground.ServerValidate
            Dim value As String

            If String.IsNullOrEmpty(args.Value) Then
                args.IsValid = True
            Else
                value = args.Value.Trim
                args.IsValid = DrawingUtils.IsValidColor(value)

                If Not args.IsValid And Not String.IsNullOrEmpty(value) Then
                    If Not value.StartsWith("#") Then
                        args.IsValid = DrawingUtils.IsValidColor("#" + value)

                        If args.IsValid Then
                            value = "#" + value
                        End If
                    End If
                End If

                tbBackground.Text = value
            End If
        End Sub

Public Shared Function IsValidColor(ByVal color As String) As Boolean
            If String.IsNullOrEmpty(color) Then
                Return True
            End If

            Try
                Dim c As Color = ColorTranslator.FromHtml(color)
                Return True
            Catch
                Return False
            End Try
        End Function
 
Try setting the validation group on the validator and buttons and the textbox.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top