Simple user control for conditional rendering of nested HTML

What I would like to do is be able to pass two attributes to a custom control, ListName and Permission, for example:

<uc:check id="uc" List="Shared Documents" Permission="OpenItems" runat="server">
  <!-- have some HTML content here that is rendered if the permission is true -->
</uc:check>

      

Then, in actual user control, there is something similar to:

<%@ Control language="C#" ClassName="check" %>
<% 
  // determine permission magic placeholder
  if (DoesUserHavePermissions(perm))
  {
    // render nested HTML content
  }
  else
  {
    // abort rendering as to not show nested HTML content
  }
%>

      

I read the page on creating a templated control on MSDN and while it works - it actually seems a bit overkill for what I'm trying to do. Is there a control that already renders content based on a Boolean expression or a simpler example template?

http://msdn.microsoft.com/en-us/library/36574bf6.aspx

Update:

In ascx, you can use the following code to simulate a very simple version:

<%@ Control Language="C#" ClassName="PermissionCheck" %>
<%@ Import Namespace="System.ComponentModel"  %>
<script runat="server">
    void Page_Init()
    {
        if (Allowed != null)
        {
            Panel container = new Panel();
            Allowed.InstantiateIn(container);
            PermissionBasedMessage.Controls.Add(container);
        }
    }

    [PersistenceMode(PersistenceMode.InnerProperty)]
    public ITemplate Allowed { get; set; }
</script>
<asp:Placeholder runat="server" ID="PermissionBasedMessage" />

      

Note. I have simplified checking the Page_Init method for this code example. Additional logical checks can be added as needed.

And link to it in the calling HTML page:

<%@ Register src="PermissionCheck.ascx" tagname="PermissionCheck" tagprefix="uc1" %>

<uc1:PermissionCheck ID="PermissionCheck1" runat="server">
  <Allowed>Allowed Access</Allowed>
</uc1:PermissionCheck>

      

+2


a source to share


4 answers


The other answers are helpful for the general form of your question, but for checking permissions, SPSecurityTrimmedControl can do what you need.



+1


a source


You can create a custom control instead of a custom control: get from the asp.net panel, add your two properties, and then only display the control if the user has the required permission. For instance. something like that:

Control (put this in App_Code for example):

namespace MyControls
{
    public class MyPanel : Panel
    {
        public string Permission { get; set; }
        public string List { get; set; }
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (UserHasPermission()) base.Render(writer);
        }
    }
}

      



Using the control:

<%@ Page ... %>
<%@ Register Namespace="MyControls" TagPrefix="mc" %>
<html>
...
    <mc:MyPanel runat="server" List="Shared Documents" Permission="OpenItems">
        put content and/or other controls here
    </mc:MyPanel>
...

      

+3


a source


Why don't you extend LiteralControl, add properties for your settings, and then display the html in the .Value of the LieralControl? Seems pretty simple and much less headache than using control patterns

+2


a source


Wrap your content with a places control and set the visibility of the control to true or false (controls that have .Visible = false will not render any html)

<asp: PlaceHolder id = "phWrapper" runat = "server">
...
</ aspen: PlaceHolder>

Then in your codeset phWrapper.Visible = DoesUserHavePermissions (perm);

Hope it helps!

+1


a source







All Articles