未在自定义验证程序中调用OnServerValidate函数。 [英] OnServerValidate function is not being called in custom validator.

查看:142
本文介绍了未在自定义验证程序中调用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 .

推荐答案

点击按钮 Page.Validate()



Call Page.Validate() on your button click :

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

   }


您在Stack Overflow上发布了相同的问题,并在评论中发现此代码是全部包含在UpdatePanel中。您提供所有相关信息至关重要,否则人们无法帮助您。这是在几个小时内发布的第二个问题,OP没有提到他们的代码在更新面板内。



asp:FileUpload在更新面板内不起作用所以你一定会遇到意想不到的行为,你需要找到一些其他的异步上传文件的解决方案。
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.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆