禁用ASP.net的TreeView复选框 [英] Disabling ASP.net treeview checkboxes

查看:202
本文介绍了禁用ASP.net的TreeView复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将你们有条件的ASP树状禁用复选框?

How would you guys conditionally disable checkboxes in an asp treeview?

例如,如果一个应用程序的用户不具有一定的权限,禁用权限项复选框,在权限树视图。

For instance, if an application user does not have a certain permission, disable that permission entry checkbox in a permissions treeview.

下面就是我要找的,这是在winform应用程序的equivaqlent(其中文本是灰色的复选框被禁用):

Here's what i'm looking for, this is the equivaqlent in a winform app (the checkboxes are disabled where the text is grayed out):

我看到那里的复选框,单击事件被拦截并忽略其他解决方案。我想preFER那里的复选框被简单地设置为禁用的解决方案。

I saw other solutions where the click event on the checkboxes is intercepted and ignored. I would prefer a solution where the checkboxes are simply set to disabled.

我在寻找一个C#解决方案,但会很乐意与一个C#/ JavaScript解决方案。

I'm looking for a C# solution but will be happy with a C#/Javascript solution.

谢谢!

推荐答案

好吧,发现了一个还算干净的解决方案是:

Ok, found a fairly clean solution to this:

在code-背后:

TreeNode newNode = new TreeNode(permission.ToString());
newNode.SelectAction = TreeNodeSelectAction.None; // no Link

    if (shouldDisableCheckbox)
    {
        // Set a class so disabled nodes can be formatted thru CSS
        // and be identifiable as disabled in Javascript.
        newNode.Text = "<span class=disabledTreeviewNode>" + newNode.Text +"</span>";
    }

nodes.Add (newNode);

在Javascript中,扫描所有树状节点为那些有类名和禁用与之关联的复选框:

in Javascript, scan all treeview nodes for those that have that className and disable the checkboxes associated to them:

    // Called via a startup script created in Code Behind.
    // Disables all treeview checkboxes that have a text with a class=disabledTreeviewNode.
    // treeviewID is the ClientID of the treeView
    function DisableCheckBoxes(treeviewID)
    {
        TREEVIEW_ID = treeviewID;

        var treeView = document.getElementById(TREEVIEW_ID);

        if (treeView)
        {
            var childCheckBoxes = treeView.getElementsByTagName("input");
            for (var i = 0; i < childCheckBoxes.length; i++)
            {
                var textSpan = GetCheckBoxTextSpan(childCheckBoxes[i]);

                if (textSpan.firstChild)
                    if (textSpan.firstChild.className == "disabledTreeviewNode")
                        childCheckBoxes[i].disabled = true;
            }
        }
    }

function GetCheckBoxTextSpan(checkBox)
{
    // Set label text to node name
    var parentDiv = checkBox.parentNode;
    var nodeSpan = parentDiv.getElementsByTagName("span");

    return nodeSpan[0];
}

这篇关于禁用ASP.net的TreeView复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆