Continuing from the previous article Securing Dynamic Data Preview 4 Refresh – Part 2 we will proceed to complete the series by finishing off the hyperlinks so you have the option of them all showing as disabled or all being hidden.
The reasoning behind this extra bit to the series (if you can call two articles a series) is I don’t like
- Dead hyperlinks when you mouse over you get the hyperlink action i.e. underline.
- I like consystancy i.e. some links hidden and some shown as dead I want it all to be the same.
So to complete this we will add a new web user control called DynamicHyperLink.ascx which will wrap the asp:DynamicHyperlink control.
%@ Control Language="C#" AutoEventWireup="true" CodeBehind="DynamicHyperLink.ascx.cs" Inherits="DD_EF_SecuringDynamicData.DynamicData.DynamicHyperLink" %> <asp:DynamicHyperLink ID="DynamicHyperLink1" runat="server" onprerender="DynamicHyperLink1_PreRender"> </asp:DynamicHyperLink> <asp:Literal ID="Literal1" runat="server"> </asp:Literal>
Listing 1 - DynamicHyperLink.ascx
public partial class DynamicHyperLink : System.Web.UI.UserControl { public String Action { get; set; } public String Text { get; set; } public String CssClass { get; set; } public Boolean ShowText { get; set; } public String DisabledClass { get; set; } protected void Page_Init(object sender, EventArgs e) { DynamicHyperLink1.Action = Action; DynamicHyperLink1.Text = Text; DynamicHyperLink1.CssClass = CssClass; } /// <summary> /// Handles the PreRender event of the DynamicHyperLink1 control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e"> /// The <see cref="System.EventArgs"/> /// instance containing the event data. /// </param> protected void DynamicHyperLink1_PreRender(object sender, EventArgs e) { // the DynamicHyperLink is considered disabled if it has no URL if (String.IsNullOrEmpty(DynamicHyperLink1.NavigateUrl)) { DynamicHyperLink1.Visible = false; Literal1.Visible = false; if (ShowText) { Literal1.Visible = true; Literal1.Text = String.Format( "<span class=\"{0}\">{1}</span> ", DisabledClass, Text); } } } }
Listing 2 - DynamicHyperLink.ascx.cs
With the DynamicHyperLink web user control we simply have some properties to allow us to pass in the main properties of the asp:DynamicHyperLink Action, Text and CssClass. The last two properties ShowText and DisbledClass are used:
- ShowText – to show the Text property in a span when the DynamicHyerLink is hidden.
- DisbledClass – is the css class to use when ShowText is true.
So this is the way it works:
When the DynamicHyperLink’s NavigateUrl is and empty string or null then the DynamicHyperLink is in the disabled state. In the disabled state if ShowText is false then no control is displayed when in a disabled state, and when ShowText is true as span is displayed surrounding the Text property and having the DisabledCalss css as the span’s class.
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="DeleteLink.ascx.cs" Inherits="DD_EF_SecuringDynamicData.DeleteLink" %> <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Delete" Text="Delete" OnClientClick='return confirm("Are you sure you want to delete this item?");' /> <asp:Literal ID="Literal1" runat="server"> </asp:Literal>
Listing 3 – DeleteHyperLink.ascx
public partial class DeleteHyperLink : System.Web.UI.UserControl { public Boolean ShowText { get; set; } public String CssClass { get; set; } public String DisabledClass { get; set; } protected void Page_Load(object sender, EventArgs e) { LinkButton1.CssClass = CssClass; // get restrictions for the current // users access to this table var table = DynamicDataRouteHandler.GetRequestMetaTable(Context); var usersRoles = Roles.GetRolesForUser(); var tableRestrictions = table.Attributes.OfType<SecureTableAttribute>(); if (tableRestrictions.Count() == 0) return; foreach (var tp in tableRestrictions) { // the LinkButton is considered disabled if delete is denied. if (tp.HasAnyRole(usersRoles) && (tp.Restriction & TableDeny.Delete) == TableDeny.Delete) { LinkButton1.Visible = false; LinkButton1.OnClientClick = null; LinkButton1.Enabled = false; Literal1.Visible = false; if (ShowText) { Literal1.Visible = true; Literal1.Text = String.Format(
"<span class=\"{0}\">Delete</span> ",
DisabledClass); } } } } }
Listing 4 – DeleteHyperLink.ascx.cs
This is pretty similar to the DynamicHyperLink.ascx the main difference being how the LinkButton is determined to be disabled, this is done by the foreach loop checking each restriction on the current table and checking to see if it is TableDeny.Delete and if so setting the disabled state.
Download
Happy coding