验证从其父页的ASP.NET用户控件 [英] Validating an ASP.NET user control from its parent page

查看:155
本文介绍了验证从其父页的ASP.NET用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,一个asp.net页面。此按钮生成并插入用户控件到页面,所以很多控件可以在一个页面上存在。我需要验证生成的内部控制在一定动态生成的控制存在。

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.

So..Page有0到N控制1的。每个控制1可以有0到N控制2的。当SaveButton点击页面上,我需要确保有每控制1内至少1控制2的。

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.

我目前两个选项:

•动态插入CustomValidators为生成每个控制,每其中将验证1控制1。

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

•执行手动验证(使用jQuery),呼吁从SaveButton.OnClientClick验证功能。

• Do the validation manually (with jQuery), calling a validation function from 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?

谢谢..?(顺便说一句 - 任何直至并包括.NET 3.5 SP1是公平的游戏)

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

推荐答案

嗯,我喜欢digiguru建议界面的想法,但因为它似乎是更合乎逻辑的地方,我会使用该接口的容器控制1对,而不是子控件对于代码的生活。我的继承人采取它:

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:

public interface IValidatableControl
{
    bool IsValidControl();    
}



然后实现这在你的控制1

then implement this on your 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.

这篇关于验证从其父页的ASP.NET用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆