扩展ASP.NET MEMBERSHIP权限功能(三 )之权限控件
在上一篇中我们完成的简易的框架,这一篇中主要是如何制作权限按钮的控件(新增,编辑,删除,打印)
扩展ASP.NET MEMBERSHIP权限功能(三 )之权限控件
(效果图)
由PermissionsButton.cs、ButtonItem.cs、ButtonItemConverter.cs组成
1.PermissionsButton.cs 继存 WebControl类
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.ComponentModel;
using System.Web.UI;
using System.Security.Permissions;
using System.Web.UI.WebControls;
using System.Drawing.Design;
using System.ComponentModel.Design;
namespace EC.Permissions.WebControls
{
/// <summary>
/// 菜单按钮控件
/// </summary>
[
AspNetHostingPermission(SecurityAction.Demand,
Level = AspNetHostingPermissionLevel.Minimal),
AspNetHostingPermission(SecurityAction.InheritanceDemand,
Level = AspNetHostingPermissionLevel.Minimal),
DefaultProperty("Buttons"),
ParseChildren(true, "Buttons"),
ToolboxData("<{0}:PermissionsButton runat=\"server\"> </{0}:PermissionsButton>"),
PersistenceMode(PersistenceMode.InnerDefaultProperty),
Description("菜单按钮控件")
]
public class PermissionsButton :WebControl
{
public string TemplateStr = "<div>{0}</div>";
private List<ButtonItem> buttons;
public PermissionsButton()
{
buttons = new List<ButtonItem>();
}
protected override void Render(HtmlTextWriter w)
{
w.Write(TemplateStr, AddButtonToHtml());
base.RenderContents(w);
}
/// <summary>
/// 按钮集合
/// </summary>
[
Category("Behavior"),
Description("按钮集合"),
Editor(typeof(CollectionEditor), typeof(UITypeEditor)),
PersistenceMode(PersistenceMode.InnerDefaultProperty)
]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public List<ButtonItem> Buttons { get { return buttons; } }
private string AddButtonToHtml()
{
StringBuilder sb = new StringBuilder();
sb.Append("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr>");
foreach (ButtonItem button in Buttons)
{
if (DAL.CheckButton(button.Type))
{
sb.Append("<td>");
sb.AppendFormat(@"<input type='button' value='{0}'>", button.Text);
sb.Append("</td>");
}
}
sb.Append("</tr></table>");
return sb.ToString();
}
}
}
2. ButtonItem.cs
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace EC.Permissions.WebControls
{
[TypeConverter(typeof(ButtonItemConverter)), Description("按钮集合")]
public class ButtonItem
{
public ButtonItem()
: this(String.Empty, PermissionType.LIST, true)
{
}
public ButtonItem(string caption, PermissionType type, bool visible)
{
this.Text = caption;
this.Type = type;
this.Visible = visible;
}
[Category("Behavior"), DefaultValue("按钮"), Description("按钮名称"), Browsable(true), NotifyParentProperty(true)]
public string Text { get; set; }
[Category("Behavior"), DefaultValue(true), Description("是否显示"), NotifyParentProperty(true)]
public bool Visible { get; set; }
[Category("Behavior"), DefaultValue(""), Description("功能类型"), NotifyParentProperty(true)]
public PermissionType Type { get; set; }
}
}
3. ButtonItemConverter.cs
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Globalization;
namespace EC.Permissions.WebControls
{
public class ButtonItemConverter : ExpandableObjectConverter
{
#region 方法
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string)) { return true; }
return base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value == null) { return new ButtonItem(); }
if (value is string)
{
string s = (string)value;
if (s.Length == 0) { return new ButtonItem(); }
return "ButtonItem";
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (value != null) { if (!(value is ButtonItem)) { throw new ArgumentException("Invalid ButtonItem", "value"); } }
if (destinationType == typeof(string)) { if (value == null) { return String.Empty; } return ((ButtonItem)value).Text; }
return base.ConvertTo(context, culture, value, destinationType);
}
#endregion
}
}
最后在webroot的web.config
中添加
<add namespace="EC.Permissions.WebControls" assembly="EC.PermissionsButton" tagPrefix="EcControls"/>
页面引用 Default.aspx
<cc1:PermissionsButton ID="PermissionsButton1" runat="server" >
<cc1:ButtonItem Text="新增" Type="ADD"/>
<cc1:ButtonItem Text="编辑" Type="EDIT" />
</cc1:PermissionsButton>
插入SQL Your RoleId请替换成你现有的RoleId
INSERT INTO [aspnet_Ex_PermissionsForRoles](RoleId,ModulePath,PermissionValue) Values('Your RoleId','/Admin/Test/default.aspx',26)
INSERT INTO [aspnet_Ex_PermissionsForRoles](RoleId,ModulePath,PermissionValue) Values('Your RoleId','/Admin/Test/ok.aspx',26)
运行页面 效果如下
将26改为8
注 数字来源于
/// 权限类型
/// </summary>
public enum PermissionType
{
ALL=0,
LIST=2,
VIEW=4,
ADD=8,
EDIT=16,
DELETE=32,
PRINT=64
}
!删除按钮没有了
将8改为32(DELETE)
按钮不见
完
下一篇 扩展ASP.NET MEMBERSHIP权限功能(三 )之页面权限
目录
扩展ASP.NET MEMBERSHIP权限功能(四 )之页面权限
扩展ASP.NET MEMBERSHIP权限功能(三 )之权限控件
扩展ASP.NET MEMBERSHIP权限功能(二 )
扩展ASP.NET MEMBERSHIP权限功能(一 )