本文介绍了未在自定义验证程序中调用OnServerValidate函数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友......我必须在我的asp表单中使用自定义验证。但未调用OnServerValidate函数



我的代码如下: -



源代码:

< asp:CustomValidator ID =cvFileUploadrunat =server

ErrorMessage =请选择文件! ControlToValidate =fuCheatingEvidence

onservervalidate =cvFileUpload_ServerValidate

ValidationGroup =vgSubmitFormValidateEmptyText =True>

< asp: FileUpload ID =fuCheatingEvidencerunat =server

宽度=890px尺寸=50/>







c#OnServerValidate代码



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

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

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

       }
       else
       {
           args.IsValid = false;
       }

   }







请告诉我什么是c#代码/源代码中的错误。




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

推荐答案

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





[]

这里我创建了一个小解决方案。它完美地工作:



[]
Here i have created one small solution. And it worked perfectly :

<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>







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;
       }

   }



这篇关于未在自定义验证程序中调用OnServerValidate函数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 17:31