简体   繁体   English

如何使用ValidationGroup创建控件并进行自定义验证?

[英]How to create a control with ValidationGroup and a custom validation?

I want to make panels Visibility true or false based on a result of a Func . 我想根据Func的结果使面板Visibility truefalse

I have a page with controls as in the following code: 我有一个包含控件的页面,如下面的代码所示:

<asp:Panel ID="Panel2" runat="server">
    <asp:Panel ID="Panel3" runat="server">
        <c:PermissionPanel ID="P1" runat="server" ValidationGroup="Val1">
            Validation Group 1 - OK
        </c:PermissionPanel>
    </asp:Panel>
</asp:Panel>

<c:PermissionPanel ID="P2" runat="server" ValidationGroup="Val1">
    Validation Group 1 - OK
</c:PermissionPanel>

<hr />

<c:PermissionPanel ID="P3" runat="server" ValidationGroup="Val2">
    Validation Group 2 - OK
</c:PermissionPanel>

<asp:Panel ID="Panel4" runat="server">
    <asp:Panel ID="Panel1" runat="server">
        <c:PermissionPanel ID="P4" runat="server" ValidationGroup="Val2">
            Validation Group 2 - OK
        </c:PermissionPanel>
    </asp:Panel>
</asp:Panel>

In short I have 4 PermissionPanel that can be inside other controls. 简而言之,我有4个PermissionPanel ,可以在其他控件中。

The code of the PermissionPanel is the following: PermissionPanel的代码如下:

public class PermissionPanel : Panel
{
    public delegate bool OnValidate();
    public event OnValidate Validate;

    public string ValidationGroup { get; set; }

    protected override void OnPreRender(EventArgs e)
    {
        this.Visible = (Validate != null ? Validate() : false);

        base.OnPreRender(e);
    }
}

I want to get all PermissionPanel s from the page and add an event on each accordingly to its group, for example: 我希望从页面中获取所有PermissionPanel ,并在每个上添加一个事件,相应于其组,例如:

protected void Page_Load(object sender, EventArgs e)
{
    // Magic code here. Linq is very welcome
    // GetPageControls<PermissionPanel>("Val1").AddEvent(() => return true);
    // GetPageControls<PermissionPanel>("Val2").AddEvent(() => return false);
}

The code above would make all panels with ValidationGroup == Val1 visible while Val2 would not be rendered. 上面的代码将使ValidationGroup == Val1所有面板都可见,而Val2将不会呈现。

So the questions are : How can I achieve this? 所以问题是 :我怎样才能做到这一点? Is there a better way of doing it? 有没有更好的方法呢?


In short I want to add a Func that will be a validation method allowing the panels to be shown or not. 总之,我想添加一个Func ,它将是一个验证方法,允许显示或不显示面板。 A real example is: 一个真实的例子是:

// If post owner is the logged user, show controls like edit and delete
() => return (user != null && user.ID == post.UserID);

There is two ways that I´m aware of: searching or self-registering . 我有两种方法可以识别: 搜索自我注册 In searching you´ll get every control in the control hierarchy starting by the page, recursively and checking if it is a PermissionPanel . 在搜索中,您将获取控件层次结构中的每个控件,从页面开始,递归并检查它是否是PermissionPanel

The second way, self-registering which I like you´ll update PermissionPanel to register itself in a list inside Page.Items and register your validation handler only for controls in this list. 第二种方式,我喜欢自我注册,你会更新PermissionPanel ,在Page.Items中的列表中注册自己,并仅为此列表中的控件注册验证处理程序。

On PermissionPanel you can do something like that: PermissionPanel您可以执行以下操作:

protected override void CreateChildControls()
{
     base.CreateChildControls();

     List <PermissionPanel> panels;

     if (Page.Items["PermissionPanels"] == null)
         Page.Items["PermissionPanels"] = panels = new List <PermissionPanel>();
     else
         panels = Page.Items["PermissionPanels"] as List <PermissionPanel>;

     panels.Add(this);
}

And on page OnPreRender you can iterate over Page.Items["PermissionPanels"] and registering validation handlers according to your validation group. 在OnPreRender页面上,您可以迭代Page.Items["PermissionPanels"]并根据您的验证组注册验证处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM