1 -- 数据库
2 use master
3 go
4 if exists(select * from sysdatabases where [name]='NestingDataList')
5 drop database NestingDataList
6 go
7
8 --创建数据库
9 create database NestingDataList
10 on primary
11 (
12 name='NestingDataList_data',
13 filename='c:\NestingDataList_data.mdf'
14 )
15 go
16 use NestingDataList
17 go
18
19 --创建系统表
20 create table sysFun
21 (
22 nodeId int not null primary key,
23 displayName varchar(50) not null,
24 parentNodeId int not null
25 )
26 go
27
28 --插入测试数据
29 insert into sysFun values(1,'人事管理',0)
30 insert into sysFun values(2,'日程管理',0)
31 insert into sysFun values(3,'通知管理',0)
32 insert into sysFun values(4,'员工管理',1)
33 insert into sysFun values(5,'部门管理',1)
34 insert into sysFun values(6,'添加员工',1)
35 insert into sysFun values(7,'添加日程',2)
36 insert into sysFun values(8,'日程查询',2)
37 insert into sysFun values(9,'我的日程',2)
38 insert into sysFun values(10,'发布通知',3)
39 insert into sysFun values(11,'通知列表',3)
40 insert into sysFun values(12,'我的通知',3)
41 go
42 select * from sysfun
43 go
44
45 --创建角色表
46
47 create table RoleRight
48 (
49 roleRightId int identity(1,1) primary key,
50 roleName varchar(50),
51 nodeId int
52 )
53 go
54 select * from roleRight
55
56 --insert into roleRight select '管理员' ,nodeId from sysFun where node in (4,5,6,1,7,8,9,2,10,11,12,3)
57
58 --NestingDataList.aspx
59
60
61
62 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="NestingDataList.aspx.cs" Inherits="_Default" %>
63
64 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
65
66 <html xmlns="http://www.w3.org/1999/xhtml" >
67 <head runat="server">
68 <title>无标题页</title>
69 </head>
70 <body>
71 <form id="form1" runat="server">
72 <div>
73
74 <%-- 外层的DataList--%>
75 <asp:DataList ID="ddlParent" runat="server" OnItemDataBound="ddlParent_ItemDataBound" >
76 <%--头模板 --%>
77 <HeaderTemplate>
78 <table style="font-size:16px; font-weight:bolder; border-bottom:1px #cccccc solid" >
79 <tr>
80 <td style="width:100px; text-align:center; ">
81 父节点</td>
82 <td style="width:400px; text-align:center;">
83 子节点</td>
84 </tr>
85 </table>
86 </HeaderTemplate>
87 <%--项模板 --%>
88 <ItemTemplate>
89 <table style="font-size:12px;border:1px #cccccc solid;">
90 <tr>
91 <%--父节点 --%>
92 <td style=" text-align:center; width:100px; font-size:14px; color:Blue; height:15px; font-weight:bold; border-right:1px #cccccc solid">
93
94 <%# Eval("displayName") %>
95 <%--父节点的Id --%>
96 <asp:HiddenField ID ="hfNodeId" Value='<%# Eval("nodeId") %>' runat="server" />
97 </td>
98 <%--子节点 --%>
99 <td style="height:15px; ">
100 <%--内嵌的DataList --%>
101 <asp:DataList ID="ddlChildNode" runat="server" RepeatColumns="5" >
102 <%--内嵌的项模板 --%>
103 <ItemTemplate>
104 <table>
105 <tr>
106 <td style=" width:100px; ">
107 <asp:CheckBox ID="childNode" Text='<%# Eval("displayName")%>' runat="server" />
108 <%--子节点的Id --%>
109 <asp:HiddenField ID ="hfChildNodeId" Value='<%# Eval("nodeId") %>' runat="server" />
110 </td>
111 </tr>
112 </table>
113 </ItemTemplate>
114
115 </asp:DataList>
116 </td>
117 </tr>
118 </table>
119 </ItemTemplate>
120
121 </asp:DataList></div>
122
123 <asp:Button ID="btnSave" runat="server" Text="选中的节点" OnClick="btnSave_Click" />
124 </form>
125 </body>
126 </html>
127
128
129 NestingDataList.aspx.cs
130
131 using System;
132 using System.Data;
133 using System.Configuration;
134 using System.Web;
135 using System.Web.Security;
136 using System.Web.UI;
137 using System.Web.UI.WebControls;
138 using System.Web.UI.WebControls.WebParts;
139 using System.Web.UI.HtmlControls;
140 using System.Data.SqlClient;
141 using System.Text;
142
143 public partial class _Default : System.Web.UI.Page
144 {
145 protected void Page_Load(object sender, EventArgs e)
146 {
147 if (!IsPostBack)
148 {
149 //父节点
150 this.ddlParent.DataSource = GetNodeByParentNodeId(0);
151 this.ddlParent.DataBind();
152 }
153 }
154
155 /// <summary>
156 /// 绑定父节点时,进行绑定子节点
157 /// </summary>
158 /// <param name="sender"></param>
159 /// <param name="e"></param>
160 protected void ddlParent_ItemDataBound(object sender, DataListItemEventArgs e)
161 {
162 if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
163 {
164 //父节点的Id
165 HiddenField hfNodeId = e.Item.FindControl("hfNodeId") as HiddenField;
166 //内嵌的DataList
167 DataList ddlChildNode = e.Item.FindControl("ddlChildNode") as DataList;
168 //绑定内嵌的datalist的数据
169 ddlChildNode.DataSource = GetNodeByParentNodeId(int.Parse(hfNodeId.Value));
170 ddlChildNode.DataBind();
171
172 }
173 }
174
175
176
177 /// <summary>
178 /// ISysFun 成员 根据节点类型查询菜单信息
179 /// </summary>
180 /// <param name="nodeId"></param>
181 /// <returns></returns>
182
183
184 public DataTable GetNodeByParentNodeId(int nodeId)
185 {
186 //查询节点,返回dataTable
187 string sqlStr = string.Format("select nodeId,displayName from sysFun where parentNodeId ={0}", nodeId);
188
189 DataTable dt = new DataTable();
190 try
191 {
192 using (SqlConnection sqlConn = new SqlConnection("Data Source=.;Initial Catalog=NestingDataList;User ID=sa;Password=1234"))
193 {
194 SqlDataAdapter adapter = new SqlDataAdapter(sqlStr, sqlConn);
195 adapter.Fill(dt);
196
197 }
198 }
199 catch (Exception ex)
200 {
201
202 string e = ex.Message.ToString();
203 }
204
205 return dt;
206
207 }
208
209
210
211 //保存
212 protected void btnSave_Click(object sender, EventArgs e)
213 {
214
215 //保存选中的节点
216 StringBuilder sbNodeId = new StringBuilder();
217 bool flag = false;
218 foreach (DataListItem dliParent in this.ddlParent.Items)
219 {
220 //父节点的Id值
221 HiddenField hfNodeId = dliParent.FindControl("hfNodeId") as HiddenField;
222
223 //内嵌的DataList
224 DataList ddlChild = dliParent.FindControl("ddlChildNode") as DataList;
225
226 foreach (DataListItem dliChild in ddlChild.Items)
227 {
228 //子节点的复选框
229 CheckBox chkChild = dliChild.FindControl("childNode") as CheckBox;
230 HiddenField hfChildNodeId = dliChild.FindControl("hfChildNodeId") as HiddenField;
231 //如果找到控制,把值保存起来
232 if (chkChild != null)
233 {
234 if (chkChild.Checked == true)
235 {
236 sbNodeId.Append(int.Parse(hfChildNodeId.Value));
237 sbNodeId.Append(",");
238 //标志父节点是否应该添加到数据库中.只要子节点选中了,父节点就应该添加到数据库中
239 flag = true;
240 }
241
242 }
243
244 }
245 //如果flag为真,则要保存父节点
246 if (flag == true)
247 {
248 sbNodeId.Append(int.Parse(hfNodeId.Value));
249 sbNodeId.Append(",");
250 flag = false;
251
252 }
253 }
254
255 //去掉最后一个","
256 string nodeIds = sbNodeId.ToString().Substring(0, sbNodeId.ToString().Length - 1);
257
258 Response.Write("选中的节点的Id " + nodeIds);
259
260
261
262 int rows = SaveRoleRight(nodeIds, "管理员");
263
264 if (rows>0)
265 {
266 Response.Write("<script>alert('保存成功.')</script>");
267 }
268 else
269 {
270 Response.Write("<script>alert('保存失败.')</script>");
271 }
272
273 }
274
275 /// <summary>
276 /// 保存
277 /// </summary>
278 /// <param name="nodeIds"></param>
279 /// <param name="roleName"></param>
280 private int SaveRoleRight(string nodeIds, string roleName)
281 {
282 int rows = 0;
283
284 //批量插入
285 string sqlStr = string.Format("insert into roleRight select '{0}' ,nodeId from sysFun where nodeId in ({1})", roleName,nodeIds);
286 try
287 {
288 using (SqlConnection sqlConn = new SqlConnection("Data Source=.;Initial Catalog=NestingDataList;User ID=sa;Password=1234"))
289 {
290 sqlConn.Open();
291 SqlCommand sqlCmd = new SqlCommand(sqlStr, sqlConn);
292
293 rows = sqlCmd.ExecuteNonQuery();
294 }
295 }
296 catch (Exception ex)
297 {
298 rows = 0;
299 string e = ex.Message.ToString();
300 }
301 return rows;
302 }
303 }
304
305