利用委托实现权限控制到按钮
在做各种应用系统时经常会涉及到权限控制,下面整理了一个权限控制到按钮的方法。
思路是:制作一排系统中要使用的按钮的控件,把按钮常用的属性加入到里面,把按钮的点击事件进行委托,在使用的是很只需拖入控件设置相应的属性即可具体按钮的控制,我只是预留了方法(需从数据库读取权限),就不写了。
Demo
btn_test.ascx
思路是:制作一排系统中要使用的按钮的控件,把按钮常用的属性加入到里面,把按钮的点击事件进行委托,在使用的是很只需拖入控件设置相应的属性即可具体按钮的控制,我只是预留了方法(需从数据库读取权限),就不写了。
Demo
btn_test.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="btn_test.ascx.cs" Inherits="Controls_btn_test" %>
<asp:Button ID="btn_1" ToolTip="按钮1" runat="Server" CommandName="an1" OnCommand="btn_OnClick">
</asp:Button>
<asp:Button ID="btn_2" ToolTip="按钮2" runat="Server" CommandName="an2" OnCommand="btn_OnClick">
</asp:Button>
btn_test.ascx.cs<asp:Button ID="btn_1" ToolTip="按钮1" runat="Server" CommandName="an1" OnCommand="btn_OnClick">
</asp:Button>
<asp:Button ID="btn_2" ToolTip="按钮2" runat="Server" CommandName="an2" OnCommand="btn_OnClick">
</asp:Button>
Code
test3.aspx<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test3.aspx.cs" Inherits="demo_test3" %>
<%@ Register Src="../Controls/btn_test.ascx" TagName="btn_test" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<script language="javascript" type="text/javascript">
function detail()
{
return confirm("我是按钮1,确认将实现后台点击事件");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<uc1:btn_test ID="Btn_test1" runat="server" Btn_1_show="YES" Btn_1_text="按钮1" Btn_2_show="YES"
Btn_2_text="按钮2" Btn_1_js="return detail();" />
</form>
</body>
</html>
test3.aspx.cs
<%@ Register Src="../Controls/btn_test.ascx" TagName="btn_test" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<script language="javascript" type="text/javascript">
function detail()
{
return confirm("我是按钮1,确认将实现后台点击事件");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<uc1:btn_test ID="Btn_test1" runat="server" Btn_1_show="YES" Btn_1_text="按钮1" Btn_2_show="YES"
Btn_2_text="按钮2" Btn_1_js="return detail();" />
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class demo_test3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Btn_test1.Click += new Controls_btn_test.ClickCmand(Btn_test1_Click);
if (!IsPostBack)
{
}
}
void Btn_test1_Click(object sender, CommandEventArgs e)
{
switch (e.CommandName.Trim())
{
case "an1":
Response.Write("按钮1点击");
break;
case "an2":
Response.Write("按钮2点击");
break;
}
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class demo_test3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Btn_test1.Click += new Controls_btn_test.ClickCmand(Btn_test1_Click);
if (!IsPostBack)
{
}
}
void Btn_test1_Click(object sender, CommandEventArgs e)
{
switch (e.CommandName.Trim())
{
case "an1":
Response.Write("按钮1点击");
break;
case "an2":
Response.Write("按钮2点击");
break;
}
}
}