ASP.NET TreeView控件回传问题

TreeView控件默认不回传TreeNodeCheckChanged事件

需要加入一个javascript函数:

function postBackObject() {
var o = window.event.srcElement;
if (o.type == "checkbox") {
__doPostBack("UpdatePanel1", "");
}
}

这里使用UpdatePanel控件实现无刷新回传效果

UpdatePanel1为UpdatePanel控件ID号


private FunctionManager functionManager = new FunctionManager();
private RoleFunctionManager roleFunctionManager = new RoleFunctionManager();
//保存所有功能
List<FunctionInfo> functions;
//保存角色所具有的功能id
List<int> functionIds;

protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["roleId"] == null)
{
Response.Redirect("RoleList.aspx");
}

if (!IsPostBack)
{
//获取所有功能
functions = functionManager.GetAllFunction();

int roleId = Convert.ToInt32(Request.QueryString["roleId"]);

//获取角色具有的功能
functionIds = roleFunctionManager.GetFunctionIdByRoleId(roleId);

//遍历FId为0的节点,即根节点
foreach (FunctionInfo function in functions.Where(p=>p.FId==0))
{
//创建一个TreeNode节点
TreeNode tn = new TreeNode();
//设置该节点显示的文本
tn.Text = function.Content;
//设置节点的值
tn.Value = function.FunctionId.ToString();
//判断当前节点是否是该角色具有的功能,如果是,则标记为选中状态
if (functionIds.Count > 0 && functionIds.Where(p => p == function.FunctionId).Count() == 1)
{
tn.Checked = true;
}
//绑定子节点,递归算法
BindChildren(tn);
//绑定到控件上
this.tvFunctions.Nodes.Add(tn);
}
}
}

private void BindChildren(TreeNode tn)
{
foreach (FunctionInfo function in functions.Where(p=>Math.Abs(p.FId)==Convert.ToInt32(tn.Value)))
{
TreeNode ctn = new TreeNode();
//设置该节点显示的文本
ctn.Text = function.Content;
//设置节点的值
ctn.Value = function.FunctionId.ToString();
//判断当前节点是否是该角色具有的功能,如果是,则标记为选中状态
if (functionIds.Count > 0 && functionIds.Where(p => p == function.FunctionId).Count() == 1)
{
ctn.Checked = true;
}
//递归
BindChildren(ctn);
tn.ChildNodes.Add(ctn);
}
}


/// <summary>
/// 复选框状态改变事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void tvFunctions_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
{
TreeNode tn=e.Node;

//if (tn.Checked)
//{
// ChangeParentCheck(tn);
// ChangeChildCheck(tn);
//}
if (tn.Checked)
{
//判断当前节点的父节点的所有兄弟节点是否都处于选中状态
ChangeParentCheck(tn);
//子节点全部选中
ChangeChildCheck(tn);
}
else
{
//判断当前节点的父节点是否处于选择状态
ChangeParentUnCheck(tn);
//子节点全部取消选中
ChangeChildUnCheck(tn);
}
}

private void ChangeParentUnCheck(TreeNode tn)
{
TreeNode ptn = tn.Parent;
//如果当前节点的父节点处于选择状态,则取消选中
if (ptn != null && ptn.Checked == true)
{
ptn.Checked = false;
ChangeParentUnCheck(ptn);
}
}

private void ChangeParentCheck(TreeNode tn)
{
TreeNode ptn = tn.Parent;
//如果存在父节点
if (ptn != null)
{
int i = 0;
//遍历所有父节点的子节点
foreach (TreeNode pc_tn in ptn.ChildNodes)
{
if (pc_tn.Checked)
{
i++;
}
}
//如果子节点都处于选中状态,则将其父节点改为选择状态
if (i == ptn.ChildNodes.Count)
{
ptn.Checked = true;
}

ChangeParentCheck(ptn);
}
}

private void ChangeChildCheck(TreeNode tn)
{
if (tn.ChildNodes.Count > 0)
{
foreach (TreeNode ctn in tn.ChildNodes)
{
ctn.Checked = true;
//递归子节点全选
ChangeChildCheck(ctn);
}
}
}

private void ChangeChildUnCheck(TreeNode tn)
{
if (tn.ChildNodes.Count > 0)
{
foreach (TreeNode ctn in tn.ChildNodes)
{
ctn.Checked = false;
ChangeChildUnCheck(ctn);
}
}
}

你可能感兴趣的:(treeview)