我用的是ASPxTreeList树形菜单控件,当我点击复选框时,整个控件上的数据都不见了,请问怎么回事?
下面是我的代码:
<dx:ASPxTreeList ID="ASPxTreeList1" runat="server" AutoGenerateColumns="False"
Height="143px" KeyFieldName="MenuId" Width="661px">
<SettingsSelection Enabled="True" AllowSelectAll="True" Recursive="True" />
<SettingsBehavior AllowFocusedNode="True" AutoExpandAllNodes="True" />
<Columns>
<dx:TreeListTextColumn Caption="菜单" FieldName="MenuName" VisibleIndex="0">
<HeaderStyle HorizontalAlign="Center" />
</dx:TreeListTextColumn>
</Columns>
</dx:ASPxTreeList>
下面是我的代码:
<dx:ASPxTreeList ID="ASPxTreeList1" runat="server" AutoGenerateColumns="False"
Height="143px" KeyFieldName="MenuId" Width="661px">
<SettingsSelection Enabled="True" AllowSelectAll="True" Recursive="True" />
<SettingsBehavior AllowFocusedNode="True" AutoExpandAllNodes="True" />
<Columns>
<dx:TreeListTextColumn Caption="菜单" FieldName="MenuName" VisibleIndex="0">
<HeaderStyle HorizontalAlign="Center" />
</dx:TreeListTextColumn>
</Columns>
</dx:ASPxTreeList>
页面部分:
<dx:ASPxTreeList ID="ASPxTreeList1" runat="server" AutoGenerateColumns="False"
Height="143px" KeyFieldName="MenuId" Width="661px"
ParentFieldName="ParentId">
<SettingsSelection Enabled="True" AllowSelectAll="True" Recursive="True" />
<SettingsBehavior AllowFocusedNode="True" AutoExpandAllNodes="True" />
<Columns>
<dx:TreeListTextColumn Caption="菜单" FieldName="MenuName" VisibleIndex="0">
<HeaderStyle HorizontalAlign="Center" />
</dx:TreeListTextColumn>
</Columns>
<Templates>
<DataCell>
<table cellpadding="0" cellspacing="0">
<tr>
<td>
<dx:ASPxImage ID="ASPxImage1" runat="server" Height="16"
ImageUrl="<%# GetIconUrl(Container) %>" IsPng="true" Width="16" />
</td>
<td>
</td>
<td style="padding-bottom: 1px;" mce_style="padding-bottom: 1px;">
<a><%# Container.Text %></a>
</td>
</tr>
</table>
</DataCell>
</Templates>
</dx:ASPxTreeList>
CS文件代码部分:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDataToTree();
}
}
/// <summary>
/// 绑定树形菜单数据源
/// </summary>
public void BindDataToTree()
{
keyNum = 0;
List<UserMenu> Alllist = bll.GetMenuRoleList(Convert.ToInt32(Request["RoleId"]));
if (Alllist != null && Alllist.Count > 0)
{
List<UserMenu> list = Alllist.FindAll(delegate(UserMenu m) { return m.ParentId == 0; }); TreeListNode fatherTree = null;//树节点
int parentId = 0;
for (int i = 0; i < list.Count; i++)
{
fatherTree = CreateNodeCore(keyNum, "RightTree", list[i].MenuName, null);
parentId = list[i].MenuId;
CreateNodes(parentId, fatherTree);//递归树
keyNum++;
}
//ASPxTreeList1.ExpandAll();//展开所有节点
this.ASPxTreeList1.ExpandToLevel(2);//默认展开2级
} }
private void CreateNodes(int parentId, TreeListNode parentNodes)
{
List<UserMenu> Alllist = bll.GetMenuRoleList(Convert.ToInt32(Request["RoleId"]));
if (Alllist != null && Alllist.Count > 0)
{
List<UserMenu> list = Alllist.FindAll(delegate(UserMenu m) { return m.ParentId == parentId; });
//递归树
for (int i = 0; i < list.Count; i++)
{
keyNum++;
//再次递归,子节点变成父节点
TreeListNode sunList = CreateNodeCore(keyNum, "RightTree", list[i].MenuName, parentNodes);
CreateNodes(list[i].MenuId, sunList);
}
} }
TreeListNode CreateNodeCore(object key, string iconName, string text, TreeListNode parentNode)
{
//添加到TreeListNode数组里
TreeListNode node = ASPxTreeList1.AppendNode(key, parentNode);
//图片名
node["IconName"] = iconName;
//节点值
node["MenuName"] = text;
return node;
}
protected string GetIconUrl(TreeListDataCellTemplateContainer container)
{
return string.Format("~/Images/{0}.png", container.GetValue("IconName"));//读取图片路径
}
运行后显示:
点击复选框显示:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDataToTree();
}
}
你把这个IsPostBack的判断去掉就可以。