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>");
}
}
走向地狱的途中,不小心走了程序员这条路,路上一个个黑心的老板,和暗无天日的加班,我才发现,通往地狱的路径中,我们这行是最短的。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义