开发者

Simple user control for conditionally rendering nested HTML

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>


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>
...


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


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


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜