添加链接权限的字段:
//权限管理 n.Target = "MainFrame"; //折叠 TreeView1.Nodes.Add(n); n.Expanded = false;
页面设计:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UserRightManger.aspx.cs" Inherits="BioErpWeb.HRSystem.UserRightManger" %> <%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %> <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %> <!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> <link href="../Styles/ERPBaseStyle.css" rel="stylesheet" type="text/css" /> <link href="../Styles/AspNetPagerStyle.css" rel="stylesheet" type="text/css" /> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <div> <asp:UpdatePanel id="up_all" runat="server"> <contenttemplate> <cc1:ModalPopupExtender ID="modal_Add" TargetControlID="btnNew" PopupControlID="Panel_Add" runat="server"> </cc1:ModalPopupExtender> <asp:ImageButton ID="imgEditBtn" Width="1" Height="1" runat="server" /> <cc1:ModalPopupExtender ID="Modal_Update" TargetControlID="imgEditBtn" PopupControlID="Panel_Update" runat="server"> </cc1:ModalPopupExtender> <div> <asp:Panel ID="Panel_Add" runat="server" CssClass="pLogo"> <table class="tb_mess"> <thead class="name"><caption>权限信息添加</caption></thead> <tr> <td>权限名</td><td> <asp:TextBox ID="txtRightName0" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtRightName0" ErrorMessage="*必须填写"></asp:RequiredFieldValidator> </td> </tr> <tr> <td>备注</td><td> <asp:TextBox ID="txtReMark0" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtReMark0" ErrorMessage="*必须填写"></asp:RequiredFieldValidator> </td> </tr> <tr><td colspan="2"></td></tr> <tr><td colspan="2"></td></tr> <tr><td colspan="2"></td></tr> <tr> <td colspan="2" class="bottomtd"> <asp:Button ID="btnAdd" runat="server" Text="提交" onclick="btnAdd_Click" /> <asp:Button ID="Button1" runat="server" Text="关闭" CausesValidation="False" UseSubmitBehavior="False" /></td> </tr> </table> </asp:Panel> </div> <div> <asp:Panel ID="Panel_Update" runat="server" CssClass="pLogo"> <table class="tb_mess"> <thead class="name"><caption>权限信息编辑</caption></thead> <tr> <td>权限名</td><td> <asp:TextBox ID="txtRightName1" runat="server"></asp:TextBox></td> </tr> <tr> <td>备注</td><td> <asp:TextBox ID="txtReMark1" runat="server"></asp:TextBox></td> </tr> <tr> <td colspan="2" class="bottomtd"> <asp:Button ID="btnUpdate" runat="server" CausesValidation="false" Text="提交" onclick="btnUpdate_Click" /><asp:Button ID="btnClose" CausesValidation="false" runat="server" Text="关闭" /></td> </tr> </table> </asp:Panel> </div> <table class="maintable"> <tr> <td class="titlebar"> 权限管理系统 </td> </tr> <tr> <td style=" text-align:right; padding-right:10px; "> <asp:Button ID="btnNew" runat="server" Text="添加" BackColor="#CDEAFB" Width="80px" Height="30px" /> </td> </tr> <tr> <td> <asp:GridView ID="GridView1" Width="100%" AutoGenerateColumns="False" runat="server" onrowcommand="GridView1_RowCommand" ShowFooter="True" DataKeyNames="ID"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:CheckBox ID="check_all" Width="50px" runat="server" /> </ItemTemplate> <FooterTemplate> <asp:LinkButton ID="lb_checkall" CausesValidation="false" runat="server" Text="全选" CommandName="allcheck"></asp:LinkButton> <asp:LinkButton ID="lb_recheckall" CausesValidation="false" CommandName="recheck" runat="server" Text="反选"></asp:LinkButton> </FooterTemplate> <FooterStyle HorizontalAlign="Center" Width="60px" /> <ItemStyle HorizontalAlign="Center" Width="50px"/> </asp:TemplateField> <asp:TemplateField HeaderText="编号"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label> </ItemTemplate> <ItemStyle HorizontalAlign="Center" /> </asp:TemplateField> <asp:TemplateField HeaderText="权限名"> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Eval("RightName") %>'></asp:Label> </ItemTemplate> <ItemStyle HorizontalAlign="Center" /> </asp:TemplateField> <asp:TemplateField HeaderText="备注"> <ItemTemplate> <asp:Label ID="Label3" runat="server" Text='<%# Eval("ReMark") %>'></asp:Label> </ItemTemplate> <ItemStyle HorizontalAlign="Center" /> </asp:TemplateField> <asp:TemplateField HeaderText="操作"> <ItemTemplate> <asp:ImageButton ID="imgEditBtn" Width="50" Height="20" CommandName="imgEdit" CommandArgument='<%#Eval("ID") %>' CausesValidation="false" ImageUrl="~/Web/images/Edit.gif" runat="server" /> <asp:ImageButton ID="imgDeleteBtn" Width="50" Height="20" CommandName="imgDelete" OnClientClick="return confirm('是否确定删除?');" CommandArgument='<%#Eval("ID") %>' ImageUrl="~/Web/images/Delete.gif" runat="server" /> </ItemTemplate> <FooterTemplate> <asp:LinkButton ID="lb_delete" runat="server" CommandName="DeleteChecked" Text="删除选中" OnClientClick="return confirm('是否确定删除?');"></asp:LinkButton> </FooterTemplate> <FooterStyle Width="150px" HorizontalAlign="Center" /> <ItemStyle HorizontalAlign="Center" /> </asp:TemplateField> </Columns> </asp:GridView> </td> </tr> <tr> <td> <webdiyer:AspNetPager ID="AspNetPager1" CssClass="paginator" CurrentPageButtonClass="cpb" runat="server" onpagechanged="AspNetPager1_PageChanged"> </webdiyer:AspNetPager> </td> </tr> </table> </contenttemplate> </asp:UpdatePanel> </div> </form> </body> </html>
权限的添加和修改的存储过程:
ALTER PROCEDURE [dbo].[tbRight_ADD] @RightName nvarchar(50), @ReMark nvarchar(50) AS INSERT INTO [tbRight]( [RightName],[ReMark] )VALUES( @RightName,@ReMark ) --------修改----- ALTER PROCEDURE [dbo].[tbRight_Update] @ID int, @RightName nvarchar(50), @ReMark nvarchar(50) AS UPDATE [tbRight] SET [RightName] = @RightName,[ReMark] = @ReMark WHERE ID=@ID
-- Description: 根据权限id获取权限信息 -- ============================================= ALTER PROCEDURE [dbo].[getRightById] @id int AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; SELECT ID, RightName, ReMark FROM tbRight WHERE ID=@id END
权限查询:
所需组件:Gridview,AspNetPager
调用分页业务方法实现分页查询
权限添加和修改:
使用Ajax组件
ModalPopupExtender
相关属性:
TargetControlID:设置触发事件的控件(如按钮)
PopupControlID:设置要显示的面板
BLL层的代码:
public class UserManagerBLL { /// <summary> /// 添加员工信息 /// </summary> /// <param name="user">user</param> public int UserMangerAdd(UserManager user) { SqlParameter[] pars = new SqlParameter[]{ new SqlParameter("@LoginName",user.LoginName), new SqlParameter("@UserName",user.UserName), new SqlParameter("@Password",user.Password), new SqlParameter("@DepartmentId",user.DepartmentId), new SqlParameter("@RoleId",user.RoleId), new SqlParameter("@Birthday",user.Birthday), new SqlParameter("@Mobile",user.Mobile), new SqlParameter("@Email",user.Email), new SqlParameter("@Photo",user.Photo), new SqlParameter("@Address",user.Address), new SqlParameter("@LastLoginDate",user.LastLoginDate), new SqlParameter("@Sex",user.Sex), new SqlParameter("@DisplayOrder",user.DisplayOrder), new SqlParameter("@Sate",user.Sate) }; return DataBaseHelper.ExcuteSqlReturnInt("UserManager_ADD", CommandType.StoredProcedure, pars); } /// <summary> /// 员工修改 /// </summary> /// <param name="user">员工对象</param> /// <returns>int</returns> public int UserManagerUpdate(UserManager user) { SqlParameter[] pars = new SqlParameter[]{ new SqlParameter("@UserId",user.UserId), new SqlParameter("@LoginName",user.LoginName), new SqlParameter("@UserName",user.UserName), new SqlParameter("@Password",user.Password), new SqlParameter("@DepartmentId",user.DepartmentId), new SqlParameter("@RoleId",user.RoleId), new SqlParameter("@Birthday",user.Birthday), new SqlParameter("@Mobile",user.Mobile), new SqlParameter("@Email",user.Email), new SqlParameter("@Photo",user.Photo), new SqlParameter("@Address",user.Address), new SqlParameter("@LastLoginDate",user.LastLoginDate), new SqlParameter("@Sex",user.Sex), new SqlParameter("@DisplayOrder",user.DisplayOrder), new SqlParameter("@Sate",user.Sate) }; return DataBaseHelper.ExcuteSqlReturnInt("UserManager_Update", CommandType.StoredProcedure, pars); } /// <summary> /// 根据指定ID返回其对象 /// </summary> /// <param name="id">userid</param> /// <returns>UserManager</returns> public UserManager getuserbyId(string id) { UserManager user = new UserManager(); SqlParameter[] pars = new SqlParameter[]{ new SqlParameter("@userid",id) }; SqlDataReader reader = DataBaseHelper.SelectSQLReturnReader("getUserByid", CommandType.StoredProcedure, pars); while (reader.Read()) { user.LoginName = reader["LoginName"].ToString(); user.UserName = reader["UserName"].ToString(); user.DepartmentId =int.Parse(reader["DepartmentId"].ToString()); user.RoleId = int.Parse(reader["RoleId"].ToString()); user.Birthday =Convert.ToDateTime(reader["Birthday"].ToString()); user.Mobile = reader["Mobile"].ToString(); user.Email = reader["Email"].ToString(); user.Photo = reader["Photo"].ToString(); user.Address = reader["Address"].ToString(); user.LastLoginDate = Convert.ToDateTime(reader["LastLoginDate"].ToString()); user.Sex = reader["Sex"].ToString() == "True" ? true : false; user.DisplayOrder =int.Parse( reader["DisplayOrder"].ToString()); user.Sate = reader["Sate"].ToString() == "True" ? true : false; } reader.Close(); return user; } }
后台代码的实现:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { pagebind(); } } public static int pageindex = 0; public static int pagesize = 10; public static string condition = ""; protected void pagebind() { this.AspNetPager1.RecordCount = SqlComm.getDataCountByCondition("tbRight", condition); this.AspNetPager1.PageSize = pagesize; this.GridView1.DataSource = SqlComm.getDataByPageIndex("tbRight", "*", "ID", condition, pageindex, pagesize); this.GridView1.DataBind(); } protected void btnAdd_Click(object sender, EventArgs e) { tbRight right = new tbRight(); UserRightMangerBLL rightbll = new UserRightMangerBLL(); right.RightName = this.txtRightName0.Text; right.ReMark = this.txtReMark0.Text; if (rightbll.UserRightAdd(right) == 0) { ScriptManager.RegisterStartupScript(this, this.GetType(), "test", "alert('提交失败')", true); } else { pagebind(); } } protected void AspNetPager1_PageChanged(object sender, EventArgs e) { pageindex = this.AspNetPager1.CurrentPageIndex - 1; pagebind(); } public static tbRight right1; public static UserRightMangerBLL userrightbll; protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "imgEdit")//编辑 { string id = e.CommandArgument.ToString(); userrightbll = new UserRightMangerBLL(); right1 = new tbRight(); right1= userrightbll.getUserRightByID(id); this.txtRightName1.Text = right1.RightName; this.txtReMark1.Text = right1.ReMark; this.Modal_Update.Show(); } if (e.CommandName == "imgDelete")//删除单条记录 { if (SqlComm.DeleteTableByCondition("dbo.tbRight", " where id='" + e.CommandArgument.ToString() + "'")) { pagebind(); } } if (e.CommandName == "allcheck") //全选 { for (int i = 0; i < this.GridView1.Rows.Count; i++) { ((CheckBox)this.GridView1.Rows[i].FindControl("check_all")).Checked = true; } } if (e.CommandName == "recheck") //反选 { for (int i = 0; i < this.GridView1.Rows.Count; i++) { if (((CheckBox)this.GridView1.Rows[i].FindControl("check_all")).Checked == true) { ((CheckBox)this.GridView1.Rows[i].FindControl("check_all")).Checked = false; } else { ((CheckBox)this.GridView1.Rows[i].FindControl("check_all")).Checked = true; } } } if (e.CommandName == "DeleteChecked")//删除选中的 { for (int i = 0; i < this.GridView1.Rows.Count; i++) { if (((CheckBox)this.GridView1.Rows[i].FindControl("check_all")).Checked == true) { SqlComm.DeleteTableByCondition("tbRight", "where ID=" + this.GridView1.DataKeys[i].Value.ToString()); } } pagebind(); } } protected void btnUpdate_Click(object sender, EventArgs e) { right1.RightName = this.txtRightName1.Text; right1.ReMark = this.txtReMark1.Text; if (userrightbll.UserRightUpdate(right1) != 0) { pagebind(); //ScriptManager.RegisterStartupScript(this, this.GetType(), "test", "alert('提交成功')", true); } }
权限的设置:
界面的代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UserRightSet.aspx.cs" Inherits="BioErpWeb.HRSystem.UserRightSet" %> <!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> <link href="../Styles/ERPBaseStyle.css" rel="stylesheet" type="text/css" /> <style type="text/css"> input{ 60px; } </style> <script src="../JS/CheckUserName.js" type="text/javascript"></script> </head> <body> <form id="form1" runat="server"> <div> <table class="maintable"> <tr> <td class="titlebar"><span>员工权限设置</span></td> </tr> <tr> <td > <asp:Label ID="lbUser" runat="server" Text="员工姓名"></asp:Label> <asp:TextBox ID="txtUserName" runat="server" ontextchanged="txtUserName_TextChanged"></asp:TextBox><asp:Button ID="btnCheckUser" Width="50px" runat="server" Text="选择" OnClientClick="showDialog()" /> </td> </tr> <tr> <td> <asp:CheckBoxList ID="cblRightList" RepeatLayout="Flow" RepeatDirection="Horizontal" runat="server"> </asp:CheckBoxList> </td> </tr> <tr> <td class="bottomtd"> <asp:Button ID="btnSave" Width="150" runat="server" Text="保存员工权限信息" onclick="btnSave_Click" /> </td> </tr> </table> </div> </form> </body> </html>
BLL层的代码:
/// <summary> /// 根据编号获取权限信息 /// </summary> /// <param name="id">id</param> /// <returns>tbRight</returns> public tbRight getUserRightByID(string id) { SqlParameter[] pars = new SqlParameter[]{ new SqlParameter("@id",int.Parse(id)) }; tbRight right = new tbRight(); SqlDataReader reader= DataBaseHelper.SelectSQLReturnReader("getRightById", CommandType.StoredProcedure, pars); if (reader.Read()) { right.ID = int.Parse(id); right.RightName = reader["RightName"].ToString(); right.ReMark = reader["ReMark"].ToString(); } reader.Close(); return right; } /// <summary> /// 给用户添加权限 /// </summary> /// <param name="tbuserright">权限值</param> /// <returns>int</returns> public int addtbUserRight(tbUserRight tbuserright) { SqlParameter[] pars = new SqlParameter[]{ new SqlParameter("@RightID",tbuserright.RightID), new SqlParameter("@UserID",tbuserright.UserID) }; return DataBaseHelper.ExcuteSqlReturnInt("tbUserRight_Add", CommandType.StoredProcedure, pars); }
UI层的代码:
public partial class UserRightSet : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { RightListBind(); } } public void RightListBind() { this.cblRightList.DataSource= SqlComm.GetDataByTableName("tbRight").Tables[0]; this.cblRightList.DataTextField = "RightName"; this.cblRightList.DataValueField = "ID"; this.cblRightList.DataBind(); } protected void txtUserName_TextChanged(object sender, EventArgs e) { if (txtUserName.Text != "请选择") { DataTable dt = SqlComm.GetDataByCondition("tbUserRight", "ID,UserID,RightID", " userid=" + this.txtUserName.Text.Trim()).Tables[0]; //每次重新选择用户后复选框列表中的数据重新设为Flase CheckBoxListReset(); //重新绑定选中员工的权限至列表 for (int j = 0; j < dt.Rows.Count; j++) { for (int i = 0; i < cblRightList.Items.Count; i++) { if (cblRightList.Items[i].Value == dt.Rows[j]["RightID"].ToString()) { cblRightList.Items[i].Selected = true; } } } } } /// <summary> /// 重置复选框列表 /// </summary> private void CheckBoxListReset() { for (int i = 0; i < cblRightList.Items.Count; i++) { cblRightList.Items[i].Selected = false; } } protected void btnSave_Click(object sender, EventArgs e) { if (this.txtUserName.Text.Trim() == "" || this.txtUserName.Text.Trim().Length == 0) { ClientScript.RegisterStartupScript(this.GetType(), "test", "<script>alert('请先选择要设置权限的用户')</script>"); return; } //删除指定用户的所有权限 DeleteUserRight(this.txtUserName.Text.Trim()); //通过循环遍历给指定用户添加相应权限值 for (int i = 0; i < cblRightList.Items.Count; i++) { if (cblRightList.Items[i].Selected) { AddUserRight(int.Parse(cblRightList.Items[i].Value.ToString()),int.Parse(this.txtUserName.Text.Trim())); } } CheckBoxListReset(); //清空值 this.txtUserName.Text = ""; } /// <summary> /// 根据ID删除指定员工权限信息 /// </summary> /// <param name="userid"></param> /// <returns></returns> private bool DeleteUserRight(string userid) { return SqlComm.DeleteTableByCondition("tbUserRight", " where UserID=" + userid); } /// <summary> /// 给用户添加一个权限信息 /// </summary> /// <param name="tbuserright">权限信息对象</param> /// <returns></returns> private int AddUserRight(int rightid,int userid) { UserRightMangerBLL userightbll = new UserRightMangerBLL(); //给用户权限对象赋值 tbUserRight tbuserright = new tbUserRight(); tbuserright.RightID = rightid; tbuserright.UserID = userid; //调用相应的业务方法实现给用户添加权限 return userightbll.addtbUserRight(tbuserright); } }
添加权限的存储过程:
-- Description: 给用户添加一条权限信息 -- ============================================= ALTER PROCEDURE [dbo].[tbUserRight_Add] @UserID int, @RightID int AS BEGIN SET NOCOUNT ON; INSERT tbUserRight ( UserID, RightID ) VALUES ( @UserID, @RightID ) END
效果: