CustomValidator没有';在asp.net中不能正常工作

本文关键字:常工作 不能 工作 net 没有 asp CustomValidator | 更新日期: 2023-09-27 18:29:35

我为FileUpload创建了一个自定义验证器,用于控制照片大小和格式

protected void cvrFileUpload_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (rbtnSelectByFile.Checked)
        {
            if (fuplBrows.HasFile)
            {
                string fileType = Path.GetExtension(fuplBrows.PostedFile.FileName).ToLower().Trim();
                if (fileType != ".jpg" && fileType != ".png" && fileType != ".bmp" && fileType != ".jpeg")
                {
                    cvrFileUpload.ToolTip = "Only .jpg, .png, .bmp file formats are allowed";
                    args.IsValid = false;
                    ScriptManager.RegisterStartupScript(this, GetType(), "pagechange", "nextPage(); ", true);
                }
                else
                {
                    if (fuplBrows.PostedFile.ContentLength > 102400)
                    {
                        cvrFileUpload.ToolTip = "حجم فایل باید کمتر از 100 کیلوبایت باشد";
                        args.IsValid = false;
                        ClientScript.RegisterStartupScript(GetType(), "pagechange", "nextPage(); ", true);
                        return;
                    }
                    else
                    {
                        args.IsValid = true;
                    }
                }
            }
        }
    }

并且有一个按钮用于将信息保存在数据库中,如果customvalidator无效,则该按钮不得工作:

protected void btnRegist_Click(object sender, EventArgs e)
        {
            ResultManage oRm = new ResultManage();
            RequestInfo oRi = form2oRi();
            int id = oRm.saveResult(oRi);
            if (id > 0)
            {
                Response.Redirect("~/RecordedResult.aspx");
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "پیام سیستم", "alert('خطا در انجام عملیات');", true);
            }
        }

当自定义验证器无效时,我不想使用我的按钮功能。我该怎么做?

CustomValidator没有';在asp.net中不能正常工作

我建议您使用

Page.IsValid 

属性以检查是否所有验证器都有效:

http://msdn.microsoft.com/en-us/library/system.web.ui.page.isvalid(v=vs.110).aspx