Treeview 简单遍历,赋值,检查 checkbox
代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
GenerateTree();
erawarner.Model.Role model = BLL.RoleManager.GetRoleById(Convert.ToInt32(Common.EncodeInputString(Request.QueryString["id"])));
lblRoleName.Text = model.frolename;
}
catch
{
Page.RegisterStartupScript("alert", "<script>alert('页面不能单独访问!')</script>");
}
}
}
private void BindTreeView(TreeNode passNode, bool isShowCheckBox)
{
IList<erawarner.Model.Menu> modelList = BLL.MenuManager.GetMenuByParentId(passNode.Value);
IList<erawarner.Model.Roleright> modelListRight = new BLL.RoleRightManager().GetModelList(" froleid = " + Common.EncodeInputString(Request.QueryString["id"]));
for (int i = 0; i < modelList.Count; i++)
{
TreeNode node = new TreeNode();
node.Text = modelList[i].fmenuname;
node.Value = modelList[i].fmenuid;
node.NavigateUrl = "#";
node.ShowCheckBox = isShowCheckBox;
foreach (erawarner.Model.Roleright item in modelListRight) // 遍历用户与角色对应关系
{
if (item.fmenuid == node.Value) // 找到用户的菜单,那么勾选上
{
node.Checked = true;
break;
}
}
passNode.ChildNodes.Add(node); // 然后添加
if (BLL.MenuManager.GetMenuByParentId(modelList[i].fmenuid).Count > 0)
{
BindTreeView(passNode.ChildNodes[passNode.ChildNodes.IndexOf(node)], isShowCheckBox);
}
}
}
private void GenerateTree()
{
tvMenu.Nodes.Clear();
tvMenu.Nodes.Add(new TreeNode("目录", "-1"));
BindTreeView(tvMenu.Nodes[0], true);
tvMenu.ExpandAll();
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
BLL.RoleRightManager.DeleteByRoleId(Convert.ToInt32(Common.EncodeInputString(Request.QueryString["id"])));
erawarner.Model.Roleright model = new erawarner.Model.Roleright();
for (int i = 0; i < tvMenu.CheckedNodes.Count; i++)
{
if (tvMenu.CheckedNodes[i].Checked)
{
model.fmenuid = tvMenu.CheckedNodes[i].Value;
model.froleid = long.Parse(Request.QueryString["id"]);
BLL.RoleRightManager.AddRoleright(model);
}
}
Page.RegisterStartupScript("alert", "<script>alert('分配成功!');window.location.href = 'RoleList.aspx'</script>");
}
catch
{
Page.RegisterStartupScript("alert", "<script>alert('分配过程中发生错误!')</script>");
}
}
{
if (!IsPostBack)
{
try
{
GenerateTree();
erawarner.Model.Role model = BLL.RoleManager.GetRoleById(Convert.ToInt32(Common.EncodeInputString(Request.QueryString["id"])));
lblRoleName.Text = model.frolename;
}
catch
{
Page.RegisterStartupScript("alert", "<script>alert('页面不能单独访问!')</script>");
}
}
}
private void BindTreeView(TreeNode passNode, bool isShowCheckBox)
{
IList<erawarner.Model.Menu> modelList = BLL.MenuManager.GetMenuByParentId(passNode.Value);
IList<erawarner.Model.Roleright> modelListRight = new BLL.RoleRightManager().GetModelList(" froleid = " + Common.EncodeInputString(Request.QueryString["id"]));
for (int i = 0; i < modelList.Count; i++)
{
TreeNode node = new TreeNode();
node.Text = modelList[i].fmenuname;
node.Value = modelList[i].fmenuid;
node.NavigateUrl = "#";
node.ShowCheckBox = isShowCheckBox;
foreach (erawarner.Model.Roleright item in modelListRight) // 遍历用户与角色对应关系
{
if (item.fmenuid == node.Value) // 找到用户的菜单,那么勾选上
{
node.Checked = true;
break;
}
}
passNode.ChildNodes.Add(node); // 然后添加
if (BLL.MenuManager.GetMenuByParentId(modelList[i].fmenuid).Count > 0)
{
BindTreeView(passNode.ChildNodes[passNode.ChildNodes.IndexOf(node)], isShowCheckBox);
}
}
}
private void GenerateTree()
{
tvMenu.Nodes.Clear();
tvMenu.Nodes.Add(new TreeNode("目录", "-1"));
BindTreeView(tvMenu.Nodes[0], true);
tvMenu.ExpandAll();
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
BLL.RoleRightManager.DeleteByRoleId(Convert.ToInt32(Common.EncodeInputString(Request.QueryString["id"])));
erawarner.Model.Roleright model = new erawarner.Model.Roleright();
for (int i = 0; i < tvMenu.CheckedNodes.Count; i++)
{
if (tvMenu.CheckedNodes[i].Checked)
{
model.fmenuid = tvMenu.CheckedNodes[i].Value;
model.froleid = long.Parse(Request.QueryString["id"]);
BLL.RoleRightManager.AddRoleright(model);
}
}
Page.RegisterStartupScript("alert", "<script>alert('分配成功!');window.location.href = 'RoleList.aspx'</script>");
}
catch
{
Page.RegisterStartupScript("alert", "<script>alert('分配过程中发生错误!')</script>");
}
}
走向地狱的途中,不小心走了程序员这条路,路上一个个黑心的老板,和暗无天日的加班,我才发现,通往地狱的路径中,我们这行是最短的。