I have an application that uses a treeview according to the image and code below.
ASPX:
<%@PageTitle="" Language="C#" MasterPageFile="~/FrontPage.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="IASD.ASCS.WebForm.register.group.Default" %>
<%@ MasterType VirtualPath="~/FrontPage.Master" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<link href="../../Content/template.css" rel="stylesheet" />
<link href="../../Content/validationEngine.jquery.css" rel="stylesheet" />
<script src="../../Scripts/jquery-1.6.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../Scripts/jquery.validationEngine.js" type="text/javascript" charset="utf-8"></script>
<script src="../../Scripts/jquery.validationEngine-pt_BR.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
function TreeNodeCheckChanged(event, control) {
// Valid for IE and Firefox/Safari/Chrome.
var obj = window.event ? window.event.srcElement : event.target;
var source = window.event ? window.event.srcElement.id : event.target.id;
source = source.replace(control.id + "t", control.id + "n");
var checkbox = document.getElementById(source);
if (checkbox != null && obj.tagName == "INPUT" && obj.type == "checkbox") {
__doPostBack(checkbox.id, "");
}
}
function validate() {
//Campo Nome
if (document.getElementById("<%=txtGroupName.ClientID%>").value.trim() == "") {
alert("Informe o Nome");
document.getElementById("<%=txtGroupName.ClientID%>").focus();
return false;
} txtGroupName
}
jQuery(document).ready(function () {
jQuery("#form1").validationEngine();
});
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div class="buttons">
<asp:ImageButton ID="btnNew" runat="server" ImageUrl="~/Images/New_4.png" OnClick="btnNew_Click" ToolTip="Novo Grupo" />
<asp:ImageButton ID="btnDelete" runat="server" ImageUrl="~/Images/exclude_G.png" OnClick="btnDelete_Click" ToolTip="Excluir Grupo" />
<asp:ImageButton ID="btnSave" runat="server" ImageUrl="~/Images/save_all.ico" OnClick="btnSave_Click" ToolTip="Salvar Grupo" ValidationGroup="vsGroup" OnClientClick=" return validate()" />
</div>
<div class="esp_campo">
<cc1:TabContainer ID="TabGroup" runat="server" TabIndex="0" Width="100%" CssClass="ajax__myTab" ActiveTabIndex="0">
<cc1:TabPanel runat="server" HeaderText="Grupo" ID="tbgroups">
<HeaderTemplate>Grupo</HeaderTemplate>
<ContentTemplate>
<div class="control-group">
<asp:Label ID="lbName" runat="server" Text="Nome"></asp:Label>
<div class="controls">
<asp:TextBox ID="txtGroupName" CssClass="textbox_search" runat="server"></asp:TextBox><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtGroupName" ErrorMessage="Preencha o campo 'Nome'!" ValidationGroup="vsGroup"></asp:RequiredFieldValidator><asp:HiddenField ID="hfGrupoID" runat="server" />
</div>
</div>
<div class="GridMain">
<asp:GridView ID="gvGroup" Width="100%" runat="server" CssClass="GridViewUser" GridLines="None"
CellPadding="4" AutoGenerateColumns="False" OnRowDeleting="gvGroup_RowDeleting"
OnRowUpdating="gvGroup_RowUpdating" ForeColor="#333333">
<AlternatingRowStyle CssClass="GridAlternativeUser" BackColor="White" />
<Columns>
<asp:BoundField HeaderText="ID" DataField="GroupID" />
<asp:BoundField HeaderText="Nome" DataField="Name" />
<asp:ButtonField ButtonType="Image" CommandName="Update" ImageUrl="~/images/edit_.png">
<FooterStyle Width="50px" />
<HeaderStyle Width="50px" />
<ItemStyle Width="50px" />
</asp:ButtonField>
<asp:ButtonField ButtonType="Image" CommandName="Delete" ImageUrl="~/images/Grids_Deletes.png" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" />
<HeaderStyle CssClass="GridHeaderGroup" BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerSettings PageButtonCount="5" />
<PagerStyle CssClass="GridPagerGroup" BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle CssClass="GridRowGroup" BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
</div>
<asp:ValidationSummary ID="vsGroup" runat="server" ValidationGroup="vsGroup" ForeColor="Red" />
</ContentTemplate>
</cc1:TabPanel>
<cc1:TabPanel runat="server" HeaderText="Menus" ID="tbMenus">
<HeaderTemplate>Menus Por Grupo</HeaderTemplate>
<ContentTemplate>
<div class="page_name">
<asp:Label ID="lblTabName" runat="server" Text="Menus do Grupo"></asp:Label>
</div>
<br />
<div class="forms">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:TreeView ID="treeMenu" runat="server" CssClass="AspNet-TreeView" ShowLines="True"
ShowCheckBoxes="All" OnTreeNodeCheckChanged="treeMenu_TreeNodeCheckChanged" ShowExpandCollapse="True"
Enabled="true">
<RootNodeStyle CssClass="AspNet-TreeView-Root" />
</asp:TreeView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="treeMenu" EventName="TreeNodeCheckChanged" />
</Triggers>
</asp:UpdatePanel>
</div>
</ContentTemplate>
</cc1:TabPanel>
</cc1:TabContainer>
</div>
</asp:Content>
Code Behind:
protected void gvGroup_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
IB.Group obj = IB.Group.GroupItem(int.Parse(gvGroup.Rows[e.RowIndex].Cells[0].Text));
hfGrupoID.Value = obj.GroupID.ToString();
txtGroupName.Text = obj.Name.ToString();
btnDelete.Visible = true;
tbMenus.Enabled = true;
TabGroup.ActiveTab = tbMenus;
WebTreeView.ClearTreeView(treeMenu.Nodes);
Business.MenuItem.MountGroupTreeView(treeMenu.Nodes, Master.listMenuGroup, null);
treeMenu.Attributes.Add("onclick", "TreeNodeCheckChanged(event, this)");
treeMenu.ExpandDepth = 0;
Business.MenuItem.CheckTreeView(treeMenu.Nodes, Business.MenuItem.ListMenubyGroup(int.Parse(hfGrupoID.Value)));
//
}
When you click on an update button, you should open the screen according to the image above so that the type of access per group is configured.
The problem that occurs is that each time the update button is clicked, the menus are duplicated by group.
The image below was clicked three times on the edit menu and the menu options by group were triplicated.
Couldanyonehelp?I'mnotsurewheretheproblemis.
Themethodthatcleansthetreeview
publicstaticvoidClearTreeView(TreeNodeCollectionNodeCollection){foreach(TreeNodeiteminNodeCollection){item.Checked=false;item.ImageUrl="";
if (item.ChildNodes.Count > 0)
ClearTreeView(item.ChildNodes);
}
}
The method that mounts the treeview
public static void MountGroupTreeView(WC.TreeNodeCollection cItens, List<MenuItem> listMenuGroup, int? ParentMenuItemID)
{
foreach (MenuItem ttItem in listMenuGroup.FindAll(p => p.ParentMenuItemID == ParentMenuItemID))
{
WC.TreeNode newItem = new WC.TreeNode(ttItem.NameMenuItem, ttItem.MenuItemID.ToString());
newItem.SelectAction = WC.TreeNodeSelectAction.None;
cItens.Add(newItem);
if (listMenuGroup.FindAll(p => p.ParentMenuItemID == ttItem.ParentMenuItemID).Count > 0)
MountGroupTreeView(newItem.ChildNodes, listMenuGroup, ttItem.MenuItemID);
}
}