views:

143

answers:

4

What I would like to do, is be able to pass two attributes to a user control, a ListName and a Permission, like so:

<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 the actual check user control, have 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 have read the page on creating a templated control on MSDN, and while that would work - it really seems to be a bit overkill for what I am trying to do. Is there a control that already renders content based on a boolean expression or a simpler template example?

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

Update:

The following code can be used in the ascx to model a very simple version of this:

<%@ 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 oversimplified the check in the Page_Init method for this sample code. Additional logic checks can be added as needed.

And reference 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: 

Why don't you extend the LiteralControl, add properties for your settings, then render html to the .Value of the LieralControl? Seems pretty simple and a lot less of a headache than using Templated controls

TheGeekYouNeed
I like this approach - I'll give it a shot and report back
Goyuix
This way you can control your output and not mess with having to set things visible = true/false.
TheGeekYouNeed
Hmmm.... can't seem to embed this in an ascx. Would really like to avoid compiled assemblies and hence, code behind as well. 'System.Web.UI.LiteralControl' is not allowed here because it does not extend class 'System.Web.UI.UserControl'.
Goyuix
post your code -- it's not correct, and I can help you fix it.
TheGeekYouNeed
+1  A: 

Wrap your content with a place holder control and set the control's visibility to true or false (controls that have .Visible = false won't render any html)

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

Then in your code-behind set phWrapper.Visible = DoesUserHavePermissions(perm);

Hope that helps!

Kant
+2  A: 

You could create a custom control instead of a user control: derive from the asp.net panel, add your two properties, then only render the control if the user has the required permission. E.g. something like this:

The 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>
...
M4N
This is the way i'd do it. Very little code needed, and you won't lose any intellisense for inner controls.
richeym
This is perfect if I wasn't in SharePoint - App_Code is a bit painful when dealing with SharePoint
Goyuix
+1  A: 

The other answers are good for the generic form of your question, but for checking permissions SPSecurityTrimmedControl might do what you need.

Tom Clarkson
This is precisely what I was looking for - thanks!
Goyuix