c# - How to create a control with ValidationGroup and a custom validation? -
i want make panels visibility
true
or false
based on result of func
.
i have page controls in 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 have 4 permissionpanel
can inside other controls.
the code of permissionpanel
following:
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 permissionpanel
s page , add event on each accordingly group, example:
protected void page_load(object sender, eventargs e) { // magic code here. linq welcome // getpagecontrols<permissionpanel>("val1").addevent(() => return true); // getpagecontrols<permissionpanel>("val2").addevent(() => return false); }
the code above make panels validationgroup
== val1
visible while val2
not rendered.
so questions are: how can achieve this? there better way of doing it?
in short want add func
validation method allowing panels shown or not. real example is:
// if post owner logged user, show controls edit , delete () => return (user != null && user.id == post.userid);
there 2 ways i´m aware of: searching or self-registering. in searching you´ll every control in control hierarchy starting page, recursively , checking if permissionpanel
.
the second way, self-registering you´ll update permissionpanel
register in list inside page.items
, register validation handler controls in list.
on permissionpanel
can that:
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"] list <permissionpanel>; panels.add(this); }
and on page onprerender can iterate on page.items["permissionpanels"]
, registering validation handlers according validation group.
Comments
Post a Comment