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

时间:2021-12-29 06:01:07

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.

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

I’m currently between two options:

我目前有两种选择:

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

•为生成的每个控件动态插入CustomValidator,每个控件都将验证一个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在内的公平游戏)

4 个解决方案

#1


8  

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.

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

#2


2  

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函数使每个控件实现相同的接口。这样,您可以将控件加载到页面上每个父控件的占位符中。提交页面时,只需循环遍历占位符中的控件,将它们转换为接口类,然后调用validate函数。我不使用自定义验证器,但您可以使用validate函数返回的对象构建验证错误列表,您可以以您喜欢的方式呈现此验证错误集合。

#3


1  

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.

然后,您应该能够在Control1的实例上指向RequiredFieldValidator。

#4


0  

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对象中,以便在回发中保留它。

#1


8  

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.

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

#2


2  

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函数使每个控件实现相同的接口。这样,您可以将控件加载到页面上每个父控件的占位符中。提交页面时,只需循环遍历占位符中的控件,将它们转换为接口类,然后调用validate函数。我不使用自定义验证器,但您可以使用validate函数返回的对象构建验证错误列表,您可以以您喜欢的方式呈现此验证错误集合。

#3


1  

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.

然后,您应该能够在Control1的实例上指向RequiredFieldValidator。

#4


0  

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对象中,以便在回发中保留它。