我为 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);
}
}
当自定义验证器无效时,我不想使用按钮功能。我该怎么做?
最佳答案
我建议你使用
Page.IsValid
检查所有验证器是否有效的属性:
http://msdn.microsoft.com/en-us/library/system.web.ui.page.isvalid(v=vs.110).aspx
关于javascript - CustomValidator 在 asp.net 中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26815104/