ASP.NET DynamicData 验证自定义图像 FieldTemplate

发布于 2024-12-27 03:31:10 字数 2305 浏览 1 评论 0原文

我创建了一个工作正常的自定义图像字段模板。我决定为图像尺寸创建某种验证,因此我创建了一个自定义验证属性。这是代码:

public class ImageSizeValidator : ValidationAttribute
{
    public long MaxSize { get; set; }
    public long MinSize { get; set; }

    public override bool IsValid(object value)
    {
        if (value == null)
            return true;

        byte[] image = (byte[]) value;

        if (image.Length / 1024L > MaxSize || image.Length / 1024L < MinSize)
            return false;

        return true;
    }
}

这是我的 FieldTemplate Image_Edit.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Image_Edit.ascx.cs" Inherits="RastinArgham.CRM.Web.DynamicData.FieldTemplates.Image_Edit" %>

<asp:FileUpload ID="FileUpload1" runat="server" OnDataBinding="FileUpload1DataBinding" />

<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator1" CssClass="DDControl DDValidator" ControlToValidate="FileUpload1" Display="Static" Enabled="false" />
<asp:DynamicValidator runat="server" ID="DynamicValidator1" CssClass="DDControl DDValidator" ControlToValidate="FileUpload1" Display="Static" />

这是 Image_Edit.ascx.cs:

public partial class Image_Edit : System.Web.DynamicData.FieldTemplateUserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SetUpValidator(RequiredFieldValidator1);
        SetUpValidator(DynamicValidator1);
    }

    protected override void ExtractValues(IOrderedDictionary dictionary)
    {
        if (FileUpload1.PostedFile == null || String.IsNullOrEmpty(FileUpload1.PostedFile.FileName) || FileUpload1.PostedFile.InputStream.Length == 0)
            return;

        dictionary[Column.Name] = FileUpload1.FileBytes;
    }

    public override Control DataControl
    {
        get
        {
            return FileUpload1;
        }
    }
}

最后是我的实体元数据类:

[Display(Name = "ServicePhoto", Order = 410, GroupName = "Misc")]
[HideColumnIn(PageTemplate.List)]
[UIHint("Image")]
[ImageSizeValidator(ErrorMessage = "Invalid Photo Size", MinSize = 30, MaxSize = 300)]
 public object ServicePhoto { get; set; }

有两个问题:

1- 当 IsValid(object value) 调用时值始终为 null!

2-当尝试上传新图像时,我总是在客户端收到“值无效”错误 验证。

I've created a custom Image Field Template which works fine. I decided to create some kind of validation for image sizes so I've created a custom validation attribute. here is the code:

public class ImageSizeValidator : ValidationAttribute
{
    public long MaxSize { get; set; }
    public long MinSize { get; set; }

    public override bool IsValid(object value)
    {
        if (value == null)
            return true;

        byte[] image = (byte[]) value;

        if (image.Length / 1024L > MaxSize || image.Length / 1024L < MinSize)
            return false;

        return true;
    }
}

and here is my FieldTemplate Image_Edit.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Image_Edit.ascx.cs" Inherits="RastinArgham.CRM.Web.DynamicData.FieldTemplates.Image_Edit" %>

<asp:FileUpload ID="FileUpload1" runat="server" OnDataBinding="FileUpload1DataBinding" />

<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator1" CssClass="DDControl DDValidator" ControlToValidate="FileUpload1" Display="Static" Enabled="false" />
<asp:DynamicValidator runat="server" ID="DynamicValidator1" CssClass="DDControl DDValidator" ControlToValidate="FileUpload1" Display="Static" />

and here is Image_Edit.ascx.cs:

public partial class Image_Edit : System.Web.DynamicData.FieldTemplateUserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SetUpValidator(RequiredFieldValidator1);
        SetUpValidator(DynamicValidator1);
    }

    protected override void ExtractValues(IOrderedDictionary dictionary)
    {
        if (FileUpload1.PostedFile == null || String.IsNullOrEmpty(FileUpload1.PostedFile.FileName) || FileUpload1.PostedFile.InputStream.Length == 0)
            return;

        dictionary[Column.Name] = FileUpload1.FileBytes;
    }

    public override Control DataControl
    {
        get
        {
            return FileUpload1;
        }
    }
}

and finally my entity meta data class:

[Display(Name = "ServicePhoto", Order = 410, GroupName = "Misc")]
[HideColumnIn(PageTemplate.List)]
[UIHint("Image")]
[ImageSizeValidator(ErrorMessage = "Invalid Photo Size", MinSize = 30, MaxSize = 300)]
 public object ServicePhoto { get; set; }

There are two problems:

1- when IsValid(object value) called value is always null!

2- when trying to upload a new image I always get "The value is not valid" Error on client side of
validation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文