aspx code:
<div>
<tbwc:TreeGrid ID="TreeGrid" runat="server" ShowHeader="true" ShowFooter="false"
Caption="用户权限修改" ExpandDepth="1" PopulateNodesFromClient="false" CaptionAlign="Left"
NodeWrap="false" LineImagesFolder="~/App_Themes/Blue_Themes/images/system/TreeViewLines"
ShowLines="true" CssClass="tgDefault" ImageSet="XPFileExplorer" NodeCellPosition="1"
Width="820px" OnNodeDataBound="TreeGrid_NodeDataBound">
<ParentNodeStyle Display="Inline" VerticalAlignEnum="NotSet" VerticalAlignType="NotSet" />
<SelectedNodeStyle Display="Inline" VerticalAlignEnum="NotSet" VerticalAlignType="NotSet" />
<RootNodeStyle Display="Inline" VerticalAlignEnum="NotSet" VerticalAlignType="NotSet" />
<Columns>
<asp:TemplateField HeaderText="序号">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ItemTemplate>
<ItemStyle Width="70px" HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="用户菜单">
<ItemTemplate>
<%# Eval("NondName")%>
</ItemTemplate>
<ItemStyle Width="400px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="权限操作">
<ItemTemplate>
<asp:CheckBoxList ID="CheckBoxList1" CellSpacing="3" Font-Size="12px" runat="server"
RepeatDirection="Horizontal">
</asp:CheckBoxList>
</ItemTemplate>
<ItemStyle Width="300px" />
</asp:TemplateField>
</Columns>
<NodeStyle Display="Inline" VerticalAlignEnum="NotSet" VerticalAlignType="NotSet" />
<HeaderStyle BackColor="Gainsboro" />
<LeafNodeStyle Display="Inline" VerticalAlignEnum="NotSet" VerticalAlignType="NotSet" />
</tbwc:TreeGrid>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="保存" />
<asp:Button ID="Button2" runat="server" Text="返回" OnClick="Button2_Click" />
<br />
</div>
CS code:
namespace Dsn_Material_Project.SystemPage
{
/// <summary>
/// 操作类型(枚举)
/// </summary>
public enum ActionType
{
浏览 = 0,
增加 = 1,
修改 = 2,
删除 = 3,
审核 = 4,
反审核 = 5,
打印 = 6
}
public partial class WebForm2 : System.Web.UI.Page
{
IList<CheckBoxList> cblList = new List<CheckBoxList>();
private static string uid = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//if (Request.QueryString["USERID"] != null)
//{
// uid = Request.QueryString["USERID"].ToString();
// Initial(uid);
//}
uid = "1";
Initial(uid);
}
}
private void Initial(string uid)
{
DataTable table = UserRightManager.GetAllUserRightById(uid);
if (table.Rows.Count > 0)
{
table.TableName = "TreeTable";
InitTreeGridManual(this.TreeGrid.Nodes, table, "0");
this.TreeGrid.ManualDataBind();
}
}
private void InitTreeGridManual(TreeGridNodeCollection treeGridNodes, DataTable dtTree, string ParentId)
{
string treeID = null;
DataRow[] treeRow = dtTree.Select("FATHER_ID=" + Convert.ToInt16(ParentId) + "");
foreach (DataRow row in treeRow)
{
TreeGridNode tgNode = new TreeGridNode(row["NODE_ID"].ToString());
treeID = row["NODE_ID"].ToString();
tgNode.Value = row["NODE_ID"].ToString();
RihtUser ruser = new RihtUser(null, row["NODE_NAME"].ToString(), Convert.ToInt32(row["NODE_ID"]), row["ROLE_ACTION"].ToString());
tgNode.DataItem = ruser;
treeGridNodes.Add(tgNode);
InitTreeGridManual(tgNode.ChildNodes, dtTree, treeID);
}
}
protected void TreeGrid_NodeDataBound(object sender, TreeGridRowEventArgs e)
{
//排除掉标题行
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label l = e.Row.Cells[1].FindControl("Label1") as Label;
l.Text = e.Row.RowIndex.ToString();
//读取当前行中的RoleAction字段
string Roles = DataBinder.Eval(e.Row.DataItem, "RoleAction").ToString();
CheckBoxList cbl = e.Row.FindControl("CheckBoxList1") as CheckBoxList;
cbl.Attributes.Add("Tag", DataBinder.Eval(e.Row.DataItem, "ModuleId").ToString());
if (string.IsNullOrEmpty(Roles))
{
return;
}
for (int i = 0; i < Roles.Length; i++)
{
if (Roles[i] == '1' || Roles[i] == '0')
{
ListItem li = new ListItem();
ActionType at = (ActionType)i;
li.Text = at.ToString();
//值是字符索引的位置
li.Value = ((int)at).ToString();
li.Selected = Roles[i] == '1' ? true : false;
cbl.Items.Add(li);
}
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
cont(TreeGrid.Controls);
if (cblList.Count == 0)
{
return;
}
int enumLength = Enum.GetNames(typeof(ActionType)).Length;
StringBuilder sbActionSql = new StringBuilder();
foreach (CheckBoxList cbl in cblList)
{
if (cbl.Items.Count == 0)
{
continue;
}
Dictionary<string, string> cblValuesdictionary = new Dictionary<string, string>();
StringBuilder sb = new StringBuilder();
foreach (ListItem li in cbl.Items)
{
cblValuesdictionary.Add(li.Value, (li.Selected == true ? 1 : 0).ToString());
}
for (int i = 0; i < enumLength; i++)
{
if (cblValuesdictionary.ContainsKey(i.ToString()))
{
sb.Append(cblValuesdictionary[i.ToString()]);
}
else
{
sb.Append("4");
}
}
sbActionSql.Append(string.Format("update USER_RIGHT set ROLE_ACTION='{0}' where USERID='{1}' and MODULE_ID={2}; ", sb.ToString(),uid, int.Parse(cbl.Attributes["Tag"])));
}
if (!UserRightManager.UpdateROLEACTION(sbActionSql.ToString()))
{
this.Page.ClientScript.RegisterStartupScript(GetType(), "scr", "<script>alert('保存成功!');location.href('UsersManage.aspx')</script>",false);
}
else
{
this.Page.ClientScript.RegisterStartupScript(GetType(), "scr", "<script>alert('保存成功!');location.href('UsersManage.aspx')</script>", false);
}
}
/// <summary>
/// 递归算法(遍历出CheckBoxList)
/// </summary>
/// <param name="sender"></param>
public void cont(ControlCollection cc)
{
foreach (Control c in cc)
{
if (c.HasControls())
{
cont(c.Controls);
}
if (c is CheckBoxList)
{
cblList.Add(c as CheckBoxList);
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("UsersManage.aspx");
}
}
}