【问题标题】:CustomValidator doesn't work correctly in asp.netCustomValidator 在 asp.net 中无法正常工作
【发布时间】:2014-11-08 08:04:57
【问题描述】:

我为 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);
            }

        }

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

【问题讨论】:

    标签: javascript c# asp.net customvalidator aspbutton


    【解决方案1】:

    我建议你使用

    Page.IsValid 
    

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

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

    【讨论】:

    • 非常感谢 :) 它有效
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-05
    • 2013-11-16
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多