locked
Custom Validator not firing OnServerValidate Sub. Help? RRS feed

  • Question

  • User1499709528 posted

    I have a DetailsView that I use for Editing and Inserting of Items.  I mostly use RequiredFieldValidators for the fields, but I have a FileUpload that lets the user upload an image so I used a CustomValidator to make sure is a valid jpeg image.  However, my validation routine is not being fired when the rest of my validators are.   Here is the pertinent apsx code:

     

    <asp:DetailsView ID="DetailsView2" runat="server" AutoGenerateRows="False" BackColor="White"

    BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" DataKeyNames="ItemID"

    DataSourceID="dsDV2" GridLines="Vertical" Height="50px" Visible="False" Width="367px" EnableViewState="False" ForeColor="Black">

    <Fields>

    <asp:TemplateField HeaderText="Item Name:" SortExpression="ItemName">

    <EditItemTemplate>

    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ItemName") %>'></asp:TextBox>

    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="TextBox1"

    ErrorMessage="Please Enter an Item Name." SetFocusOnError="True" ValidationGroup="DV2">*</asp:RequiredFieldValidator>

    </EditItemTemplate>

    <InsertItemTemplate>

    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ItemName") %>'></asp:TextBox>

    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="TextBox1"

    ErrorMessage="Please enter an Item Name." SetFocusOnError="True" ValidationGroup="DV2">*</asp:RequiredFieldValidator>

    </InsertItemTemplate>

    </asp:TemplateField>

    <asp:TemplateField HeaderText="Image File:" SortExpression="ItemFileName">

    <EditItemTemplate>

    <asp:FileUpload ID="FileUpload1" runat="server" Visible="False" />&nbsp;

    <asp:CustomValidator ID="CustomValidator2" runat="server" OnServerValidate="ValidateImage" ControlToValidate="FileUpload1"

    ErrorMessage="Image File is not a valid image." ValidateEmptyText="True" ValidationGroup="DV2">*</asp:CustomValidator>

    </EditItemTemplate>

    <InsertItemTemplate>

    &nbsp;<asp:FileUpload ID="FileUpload2" runat="server" Width="200px" />

    <asp:CustomValidator ID="CustomValidator1" runat="server" OnServerValidate="ValidateImage" ControlToValidate="FileUpload2"

    ErrorMessage="Image File is not a valid JPEG image." ValidationGroup="DV2" ValidateEmptyText="True">*</asp:CustomValidator>

    </InsertItemTemplate>

    </asp:TemplateField>

    </Fields>

    </asp:DetailsView>

    <asp:Button ID="BtnDV1" runat="server" Text="Update" Visible="False" ValidationGroup="DV2" />

     

    and the code behind for the "ValidateImage" sub:

     

    Protected Sub ValidateImage(ByVal sender As Object, ByVal args As ServerValidateEventArgs)

    Dim fu As FileUpload

    If Me.DetailsView2.CurrentMode.ToString.Equals("Edit") Then

    fu = Me.DetailsView2.FindControl("FileUpload1")

    Else

    fu = Me.DetailsView2.FindControl("FileUpload2")

    End If

    If fu.PostedFile.FileName.ToString.Equals("") Then

    args.IsValid = True

    Exit Sub

    Else

    Select Case fu.PostedFile.ContentType

    Case "image/pjpeg", "image/jpeg" 'Make sure we are getting a valid JPG image

    args.IsValid = True

    Case Else

    'Not a valid jpeg image

    args.IsValid = False

    End Select

    End If

    End Sub

     

    Can anyone tell me why the app never fires this routine?  I've searched online and I'm pretty sure I've done everything right.  Any help would be greatly appreciated!

    Thursday, November 29, 2007 11:57 AM

Answers

  • User77559874 posted

    Hello,

    Yes, this RegularExpressionValidator will only check for the file extension. So you can check that at client end and if the file is valid then you can apply your logic of getting the file size and display a general message using a label or div.

    Thanks,
    Deepesh Verma

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 30, 2007 1:35 PM

All replies

  • User77559874 posted

     Hello,

    You can use RegularExpressionValidator for validation the File Upload control

     <asp:RegularExpressionValidator ID="regexResume" runat="server" Display="Dynamic"
    Text="Only .jpg, .jpeg file formats are allowed."
    ControlToValidate="FileUpload" ValidationExpression="^([a-zA-Z].*|[1-9].*)\.(((j|J)(p|P)(e|E)(g|G))|((j|J)(p|P)(g|G)))$"
    SetFocusOnError="True"></asp:RegularExpressionValidator>

    Thanks,

    Deepesh Verma 

     

    Friday, November 30, 2007 8:57 AM
  • User1499709528 posted

    Thanks for the response.  I am about to try that (looking up the expression syntax to customize it a little more).  However, I don't believe it will ultimately work because I was also going to add a size-checker to my codebehind, to make sure the image's file size was not too large.  So doing this RegularExpressionValidator will not allow me to do this, correct?

    Friday, November 30, 2007 10:17 AM
  • User77559874 posted

    Hello,

    Yes, this RegularExpressionValidator will only check for the file extension. So you can check that at client end and if the file is valid then you can apply your logic of getting the file size and display a general message using a label or div.

    Thanks,
    Deepesh Verma

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 30, 2007 1:35 PM
  • User1303816130 posted

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Text_Edit.ascx.cs" Inherits="MyNS.Controls.Text_Edit" %>
    <asp:TextBox ID="RegexTextBox" runat="server" CssClass="text" Text=""  
        Columns="10"></asp:TextBox>
    <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Validation Failed"
        CssClass="text" ControlToValidate ="RegexTextBox" Display="Dynamic" Enabled="false"
        BorderStyle="None" OnServerValidate="serverValidate">Validation Failed</asp:CustomValidator>
    
    
     code behind:
         protected void serverValidate(object src, ServerValidateEventArgs arg)
            {
                CustomValidator1.ErrorMessage = "Text lenght should be greater than 8";
                arg.IsValid = false;
            }

    I also enable the customvalidator through exposed property
       public string ControlToCompare
            {
                get { return CustomValidator1.ToolTip; }
                set
                {
                    CustomValidator1.ToolTip = value;
                    CustomValidator1.Enabled = true;
                }
            }
     I am in kind of the same situation, somehow I couldnt reason out why my server side validation is not firing, any help on figuring it out very much appreciated.

    I am validating a content of a textbox, i do have value inside the textbox, and I am setting the errormessage of custom validator. So the standard reasoning for customvalidators is not the cause. To get into the details, I am having the textbox and custom validator in a usercontrol, I have tested the usercontrol seperately by adding to a page, the custom validator properly fires up the servervalidate function. The problem comes up when I dynamically add the user control to a page on its page init, all the client side validations controls fire properly but the server validation is not working. When I dynamically add the usercontrol I call the property and i can see that customvalidator is getting enabled. But I can never get the debug to hit the breakpoint I had set on servervalidate method that i had written on the controls code behind.

    Thursday, October 9, 2008 11:34 PM