Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello friends ... i have to use custom validation in my asp form . but OnServerValidate function is not being called.

my code is below :-

source code :
<asp:CustomValidator ID="cvFileUpload" runat="server"
ErrorMessage="Please select file!" ControlToValidate="fuCheatingEvidence"
onservervalidate="cvFileUpload_ServerValidate"
ValidationGroup="vgSubmitForm" ValidateEmptyText="True">
<asp:FileUpload ID="fuCheatingEvidence" runat="server"
Width="890px" Size="50" />



c# OnServerValidate code

C#
protected void cvFileUpload_ServerValidate(object source, ServerValidateEventArgs args)
   {
       String fileName = fuCheatingEvidence.PostedFile.FileName;

       if (fileName != "")
       {
           args.IsValid = true;

       }
       else
       {
           args.IsValid = false;
       }

   }



please tell me what is the error in the c# code/source code .
Posted

Call Page.Validate() on your button click :

C#
protected void yourButton_Click(Object sender, EventArgs e)
{
    Page.Validate();
    if(Page.IsValid)
    {
       // write your code here
    }
}


[[Edit]]
Here i have created one small solution. And it worked perfectly :
ASP.NET
<form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="fuCheatingEvidence" runat="server" Width="890px" Size="50" />
        <asp:CustomValidator ID="cvFileUpload" runat="server" ErrorMessage="Please select file!"
            ControlToValidate="fuCheatingEvidence" OnServerValidate="cvFileUpload_ServerValidate"
            ValidationGroup="vgSubmitForm" ValidateEmptyText="True"></asp:CustomValidator>
            <asp:Button ID="bthUpload" runat="server" Text="Upload" ValidationGroup="vgSubmitForm" OnClick="bthUpload_Click" />
    </div>
    </form>


C#
protected void bthUpload_Click(object sender, EventArgs e)
   {
       //your code for uploading
   }

   protected void cvFileUpload_ServerValidate(object source, ServerValidateEventArgs args)
   {
       String fileName = fuCheatingEvidence.PostedFile.FileName;

       if (fileName != "")
       {
           args.IsValid = true;

       }
       else
       {
           args.IsValid = false;
       }

   }
 
Share this answer
 
v2
Comments
Ashutosh shukla207 12-Jan-16 5:11am    
have i call it on ClientValidationFunction ? in custom validator
Raje_ 12-Jan-16 5:13am    
No you should check this in your code behind.
Ashutosh shukla207 12-Jan-16 5:17am    
its not working
Raje_ 12-Jan-16 5:20am    
I see you are validating fileupload. Is your fileupload control in UpdatePanel? If it is true. then file upload control will not work inside update panel.
Ashutosh shukla207 12-Jan-16 5:23am    
i didn't use any update panel... and inside update panel i know upload file with help of postback trigger.but validation not triggered. this is first time in my carrier
You posted the same question on Stack Overflow and in the comments it transpires that this code is all wrapped inside an UpdatePanel. It is vital you provide all relevant information or else people can't help you. This is the second question posted in a matter of hours where the OP does not mention their code is inside an update panel.

asp:FileUpload does not work inside an updatepanel so you're bound to encounter unexpected behaviour, you'll need to find some other solution to asynchronously upload files.
 
Share this answer
 
Comments
Ashutosh shukla207 12-Jan-16 5:07am    
i didn't use any update panel... and inside update panel we can upload file with help of postback trigger. that is another thing . the thing is that why valiadtion is not fired when there is no update panel in the form

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900