您好,登錄后才能下訂單哦!
--------------------------------------------------LimitAttribute.cs 自定義特性
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// LimitAttribute 的摘要說明 /// </summary> //特性只作用與方法上 [AttributeUsage(AttributeTargets.Method)] public class LimitAttribute:Attribute { private string _name; public string Name { get { return _name; } } public LimitAttribute(string name) { this._name = name; } }
--------------------------------------------------Default.aspx
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Reflection; public partial class _Default : System.Web.UI.Page { public static readonly string _name = "李四"; protected void Page_Load(object sender, EventArgs e) { } /// <summary> /// 判斷該用戶是否有執行權限 /// </summary> /// <param name="name">用戶名稱</param> /// <param name="method">方法名稱</param> /// <returns></returns> private bool IsLimit(string name, string method) { //獲取該類型 Type t = typeof(_Default); //查找該方法 MethodInfo mi = t.GetMethod(method); if (mi == null) return false; //獲取方法上的特性 LimitAttribute la = Attribute.GetCustomAttribute(mi, typeof(LimitAttribute)) as LimitAttribute; if (la == null) return false; //判斷用戶 if (la.Name == name) { return true; } else { return false; } } [Limit("張三")] public void btnView_Click(object sender, EventArgs e) { if (IsLimit(_name, "btnView_Click")) { ClientScript.RegisterStartupScript(this.GetType(), "ClientScript", "alert('我是查看')", true); } else { ClientScript.RegisterStartupScript(this.GetType(), "ClientScript", "alert('對不起,您沒有權限操作')", true); } } [Limit("李四")] public void btnEdit_Click(object sender, EventArgs e) { if (IsLimit(_name, "btnEdit_Click")) { ClientScript.RegisterStartupScript(this.GetType(), "ClientScript", "alert('我是修改')", true); } else { ClientScript.RegisterStartupScript(this.GetType(), "ClientScript", "alert('對不起,您沒有權限操作')", true); } } [Limit("李四")] public void btnAdd_Click(object sender, EventArgs e) { if (IsLimit(_name, "btnAdd_Click")) { ClientScript.RegisterStartupScript(this.GetType(), "ClientScript", "alert('我是添加')", true); } else { ClientScript.RegisterStartupScript(this.GetType(), "ClientScript", "alert('對不起,您沒有權限操作')", true); } } [Limit("王五")] public void btnDel_Click(object sender, EventArgs e) { if (IsLimit(_name, "btnDel_Click")) { ClientScript.RegisterStartupScript(this.GetType(), "ClientScript", "alert('我是刪除')", true); } else { ClientScript.RegisterStartupScript(this.GetType(), "ClientScript", "alert('對不起,您沒有權限操作')", true); } } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。