說明:有使用MasterPage,想根據用戶的權限代號(Authority)管控頁面上的新增/編輯/刪除等button,無權限則disable該button。
做法:在MasterPage的PreRender事件中使用ContentPlaceHolder控制項的findcontrol找到新增/編輯/刪除等button後,再分別disable該button。例:
//namespace
[Flags]
public enum Privilege
{
None=0,
View=1,
Add=2,
Edit=4,
Remove=8,
All = None|View|Add|Edit|Remove
}
//xxx.master.cs
protected override void OnPreRender(EventArgs e)
{
try
{
Button btnAdd = (Button)ContentPlaceHolder1.FindControl("btnAdd");
Button btnEdit = (Button)ContentPlaceHolder1.FindControl("btnEdit");
Button btnDel = (Button)ContentPlaceHolder1.FindControl("btnDel");
if (Authority == (int)Privilege.View)
{
btnAdd.Disable = false;
btnEdit.Disable = false;
btnDel.Disable = false;
}
else if (Authority == (int)(Privilege.View | Privilege.Add))
{
btnEdit.Disable = false;
btnDel.Disable = false;
}
...以此類推
}
catch (Exception ex)
{
throw ex;
}
base.OnPreRender(e);
}
參考資料:
沒有留言:
張貼留言