C# 从父页面验证 ASP.NET 用户控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/255317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:09:36  来源:igfitidea点击:

Validating an ASP.NET user control from its parent page

c#asp.netvalidationuser-controlscustom-controls

提问by JoeB

I have an asp.net page with a button. This button generates and inserts a user control into the page, so many controls could exist on one page. I need to validate that a certain dynamically generated control inside the generated control exists.

我有一个带按钮的asp.net 页面。此按钮会生成一个用户控件并将其插入到页面中,因此一个页面上可以存在多个控件。我需要验证生成的控件中是否存在某个动态生成的控件。

So..Page has 0 to N Control1's. Each Control 1 can have 0 to N Control2's. When SaveButton is clicked on Page, I need to make sure there are at least 1 Control2's inside every Control1.

所以..Page有0到N个Control1。每个 Control 1 可以有 0 到 N 个 Control2。当在页面上单击 SaveButton 时,我需要确保每个 Control1 内至少有 1 个 Control2。

I'm currently between two options:

我目前有两个选择:

? Dynamically insert CustomValidators for each control that is generated, each of which would validate one Control1.

? 为每个生成的控件动态插入 CustomValidators,每个控件都会验证一个 Control1。

? Do the validation manually (with jQuery), calling a validation function from SaveButton.OnClientClick.

? 手动执行验证(使用 jQuery),从 SaveButton.OnClientClick 调用验证函数。

Both are sloppy in their own way – which is why I'm sharing this with you all. Am I missing the easy solution?

两者都以自己的方式草率 - 这就是为什么我要与大家分享这一点。我错过了简单的解决方案吗?

Thanks in advance.. (btw – anything up to and including .NET 3.5 SP1 is fair game)

提前致谢..(顺便说一句 - 任何高达并包括 .NET 3.5 SP1 的东西都是公平的游戏)

采纳答案by SecretDeveloper

Hmm i like the Interface idea suggested by digiguru but i would use the interface on the container Control1 instead of the sub controls as it seems like the more logical place for the code to live. Heres my take on it:

嗯,我喜欢 digiguru 建议的接口想法,但我会使用容器 Control1 上的接口而不是子控件,因为它似乎是代码存在的更合乎逻辑的地方。这是我的看法:

public interface IValidatableControl
{
    bool IsValidControl();    
}

then implement this on your Control1

然后在你的 Control1 上实现这个

public class Control1 : IValidatableControl
{
... Other methods
    public bool IsValidControl()
    {

        foreach(object c in this.Controls)
        {
            if(c.GetType() == "Control2")
                return true;
        }
        return false;
    }

}

There are probably better ways to write this but it should give you enough of an idea to get started.

可能有更好的方法来写这个,但它应该给你足够的想法开始。

回答by tbreffni

One method you could try is creating and maintaining a simple xml structure that represents your custom control hierarchy. Insert or delete from this structure any time you create or destroy a custom user control. Upon save, validate that the control hierarchy represented in the xml structure is correct. You could save the xml in the Session object to persist it across postbacks.

您可以尝试的一种方法是创建和维护一个简单的 xml 结构来表示您的自定义控件层次结构。任何时候创建或销毁自定义用户控件时,都可以从该结构中插入或删除。保存时,验证 xml 结构中表示的控件层次结构是否正确。您可以将 xml 保存在 Session 对象中,以便在回发之间保持它。

回答by digiguru

If you are adding user controls on the fly, you could make each control implement the same interface with a Validate function. That way you can load the controls into a placeholder in each parent control on the page. When the page is submitted, simply loop through the controls in the placeholder, cast them to the interface class and then call the validate function. I doesn't use custom validators, but you can build up a list of validation errors using the object returned from the validate function, you can render this collection of validation errors whichever way you like.

如果您正在动态添加用户控件,您可以使每个控件使用 Validate 函数实现相同的接口。这样您就可以将控件加载到页面上每个父控件的占位符中。提交页面时,只需循环遍历占位符中的控件,将它们转换为接口类,然后调用验证函数即可。我不使用自定义验证器,但您可以使用从验证函数返回的对象来构建验证错误列表,您可以以任何您喜欢的方式呈现此验证错误集合。

回答by Kelly Adams

I think you could do it by assigning a public property in Control1 that references the existence of Control2's ID, and then decorate Control1's class with ValidationProperty. I'm thinking something along these lines:

我认为你可以通过在 Control1 中分配一个公共属性来引用 Control2 的 ID 的存在,然后用 ValidationProperty 装饰 Control1 的类。我在想这些事情:

[ValidationProperty("Control2Ref")]
public partial class Control1 : UserControl
{
    public string Control2Ref
    {
        get { return FindControl("Control2"); }
    }
    // rest of control 1 class
}

And then you should be able to point a RequiredFieldValidator at an instance of Control1.

然后您应该能够将一个RequiredFieldValidator 指向Control1 的一个实例。