一、如何实现将角色与任务的关系写进数据库中?
例如,我在界面左边的GridView控件上选定了“系统管理员”,然后在右边的GridView控件上勾选了上面的三个选项,然后按“更新”按钮,在“更新”按钮的click事件中如何用代码将“系统管理员”的ID与选定任务的ID写进数据库中?
二、如何实现在该页面中,选择了某一角色后,右边的GridView控件根据数据表中该角色的任务极限在相应的CheckBox 打上勾?
[此贴子已经被作者于2007-7-30 9:38:17编辑过]
[此贴子已经被作者于2007-7-30 9:38:17编辑过]
前台代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Role-Task.aspx.cs" Inherits="Admin_User_Role_Task_Role_Task" %>
<!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>
<style type="text/css">
.hidden
{display:none;}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
</div>
<div style="z-index: 101; left: 19px; width: 232px; position: absolute; top: 5px;
height: 242px">
<asp:Label ID="Label1" runat="server" Text="角色列表"></asp:Label><br />
<asp:Panel ID="Panel2" runat="server" Height="500px" BorderColor="PeachPuff" BorderStyle="Double" ScrollBars="Auto">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="#DEBA84"
BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2"
EmptyDataText="数据不存在……" Font-Size="Smaller" GridLines="None" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowCreated="GridView1_RowCreated" OnRowDeleting="GridView1_RowDeleting"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Width="250px">
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<Columns>
<asp:BoundField DataField="RoleId" HeaderText="ID" ReadOnly="True">
<ItemStyle CssClass="hidden" Width="0px" />
<HeaderStyle CssClass="hidden" Width="0px" />
</asp:BoundField>
<asp:TemplateField HeaderText="角色名称">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("RoleName") %>' TextMode="MultiLine"
Width="168px"></asp:TextBox>
</EditItemTemplate>
<ItemStyle Width="180px" />
<HeaderStyle BackColor="#FFFFC0" ForeColor="Black" />
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("RoleName", "{0}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
Text="更新"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
Text="取消"></asp:LinkButton>
<ajaxToolkit:ConfirmButtonExtender ID="ConfirmButtonExtender1" runat="server" ConfirmText="确要取消当前操作?"
TargetControlID="LinkButton2">
</ajaxToolkit:ConfirmButtonExtender>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="40px" />
<HeaderStyle Width="40px" />
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"
Text="编辑"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete"
Text="删除"></asp:LinkButton>
<ajaxToolkit:ConfirmButtonExtender ID="ConfirmButtonExtender2" runat="server" ConfirmText="确要删除数据?"
TargetControlID="LinkButton2">
</ajaxToolkit:ConfirmButtonExtender>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowSelectButton="True" />
</Columns>
<RowStyle BackColor="#E3EAEB" ForeColor="#8C4510" Wrap="False" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="Blue" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="OldLace" BorderStyle="None" />
</asp:GridView>
<br />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</asp:Panel>
<table cellpadding="2" cellspacing="1">
<tr>
<td style="width: 3px">
<asp:TextBox ID="TextBox2" runat="server" Width="168px"></asp:TextBox></td>
<td style="width: 3px">
<asp:Button ID="Button1" runat="server" Text="添加角色" OnClick="Button1_Click" /></td>
</tr>
</table>
</div>
<div style="z-index: 102; left: 324px; width: 100px; position: absolute; top: 5px;
height: 100px">
<asp:Label ID="Label2" runat="server" Text="允许执行的任务列表" Width="200px"></asp:Label><br />
<asp:Panel ID="Panel1" runat="server" Height="500px" ScrollBars="Auto" BorderColor="PeachPuff" BorderStyle="Double">
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" BackColor="#DEBA84"
BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2"
EmptyDataText="数据不存在……" Font-Size="Smaller" GridLines="None"
OnRowCreated="GridView1_RowCreated" Width="250px">
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<Columns>
<asp:BoundField DataField="GongNengMiaoShu_Id" HeaderText="ID" ReadOnly="True">
<ItemStyle CssClass="hidden" Width="0px" />
<HeaderStyle CssClass="hidden" Width="0px" />
</asp:BoundField>
<asp:TemplateField HeaderText="允许">
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="30px" />
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="GongNengMiaoShu" HeaderText="功能描述" />
</Columns>
<RowStyle BackColor="#E3EAEB" ForeColor="#8C4510" Wrap="False" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="Blue" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="OldLace" BorderStyle="None" />
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button2" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</asp:Panel>
<asp:Button ID="Button2" runat="server" Text="更新" /></div>
</form>
</body>
</html>
后台部分代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection conn = new SqlConnection("data source=(local);database=****;user=****; password=****");
DataSet ds = new DataSet();
SqlDataAdapter da1 = new SqlDataAdapter("select * from roles", conn);
da1.Fill(ds, "roles");
SqlDataAdapter da2 = new SqlDataAdapter("select * from Authority", conn);
da2.Fill(ds, "Authority");
GridView1.DataSource = ds;
GridView1.DataMember = "roles";
GridView1.DataBind();
GridView2.DataSource = ds;
GridView2.DataMember = "Authority";
GridView2.DataBind();
}
}
谢谢两位斑竹的热心教导,还有问题请教:
问题一:
斑竹的第一点意思是:在角色与任务关系表中,将某个角色的所拥有的所有任务权限放在一起,用逗号隔分(一个角色对应一个任务数组)还是在数据表一个角色ID对应一个任务ID?
例如:
ID RoleId TaskId
1 1 1,2,4,6
2 2 3,5,7
还是
ID RoleId TaskId
1 1 1
2 1 2
3 1 4
4 1 6
5 2 3
6 2 5
7 2 7
问题二
以上不管采用哪一种方式存放权限,当查出角色下拥有的权限数据后,如何实现遍历右边的GridView与查询出来的数据进行匹配,满足的就把相应行的CheckBox打勾?
谢谢斑竹!
问题一 中如果使用第二种方式,在“更新权限”时需将数据库中该角色的所有数据行删除,然后再重新添加该角色与所有任务的关系。以下的代码实现了将权限重新写入数据表,不知是否还有更加好的方法。
CheckBox chk;
foreach (GridViewRow r in GridView2.Rows)
{
chk = (CheckBox)r.FindControl("Checkbox1");
if (chk != null)
{
if (chk.Checked)
{
DataRow addrow = ds1.Tables[0].NewRow();
//addrow[1] = Session["JsId"].ToString();
addrow["RoleId"] = GridView1.SelectedRow.Cells[0].Text.Trim();
addrow["GongNeng_Id"] = r.Cells[0].Text.Trim();
ds1.Tables[0].Rows.Add(addrow);
da1.Update(ds1, "relRoleAndAuthority");
}
}
}
问题二 ,如何将从数据库筛选出来的数据放在 ArrayList 中?以下代码不会怎样写了(红色部分),请帮忙,谢谢!
SqlConnection conn = new SqlConnection("data source=(local);database=****;user=****; password=****");
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("select GongNeng_Id from relRoleAndAuthority where RoleId = " + GridView1.SelectedRow.Cells[0].Text, conn);
da.Fill(ds, "Authority");
ArrayList MyList = new ArrayList();
MyList = ds.Tables["Authority"].Rows;
????
foreach (GridViewRow row in GridView2.Rows)
{
if (MyList.Contains(row.Cells[1].Text))
{
CheckBox checkBox = (CheckBox)row.FindControl("CheckBox1");
checkBox.Checked = true;
}
}
[此贴子已经被作者于2007-7-31 11:10:47编辑过]
谢谢!还有一个问题:如何一次性将GridView中所有的CheckBox均处于非选定状态?以下是笨办法:
foreach (GridViewRow row in GridView2.Rows)
{
CheckBox checkBox = (CheckBox)row.FindControl("CheckBox1");
checkBox.Checked = false;
if (MyList.Contains(row.Cells[0].Text))
{
checkBox.Checked = true;
}
}
[此贴子已经被作者于2007-7-31 11:52:20编辑过]