【问题标题】:How to make a custom control validatable?如何使自定义控件可验证?
【发布时间】:2012-05-09 17:37:05
【问题描述】:

我有一个基于Panel 的自定义控件。它只是一个带有 3 个 DropDownLists 的简单面板。如果我将CompareValidator 添加到我的WebForm 并将ControlToValidate 属性设置为我的自定义控件的ID,我会收到以下错误:

Control '{0}' referenced by the ControlToValidate property of '{1}' cannot be validated.

我明白为什么,CompareValidator 无法根据Panel 合理地验证任何内容。那么我需要向我的自定义控件添加什么才能使验证器能够对其进行验证呢?

我尝试将Text 属性添加到我的面板,它返回3 个下拉列表的文本属性的组合。没用。

我已尝试寻找可验证控件 (TextBox) 实现的某些接口 (ITextControl) 并将它们添加到我的自定义控件中。没用。

如何使我的基于面板的自定义控件可由常规 .net 验证器验证? (RequiredFieldValidator、CompareValidator等)

【问题讨论】:

    标签: c# asp.net validation


    【解决方案1】:

    如果您还没有它,您需要将ValidationProperty 属性添加到您的类中,以便它知道使用哪个属性进行验证:

    [ValidationProperty("Text")]
    [ToolboxData("<{0}:YourCustomControl runat=server></{0}:YourCustomControl>")]
    public class YourCustomControl : WebControl
    ...
    

    【讨论】:

    • 是的,这就行了!谢谢你们的快速响应。 :) 将在几分钟内接受。
    猜你喜欢
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多