简体   繁体   English

禁用ASP.net树视图复选框

[英]Disabling ASP.net treeview checkboxes

How would you guys conditionally disable checkboxes in an asp treeview? 你们如何有条件地禁用asp树视图中的复选框?

For instance, if an application user does not have a certain permission, disable that permission entry checkbox in a permissions treeview. 例如,如果应用程序用户没有特定权限,请在权限树视图中禁用该权限条目复选框。

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): 这是我正在寻找的,这是winform应用程序中的等价物(禁用复选框,文本显示为灰色):

在此输入图像描述

I saw other solutions where the click event on the checkboxes is intercepted and ignored. 我看到了其他解决方案,其中复选框上的click事件被截获并被忽略。 I would prefer a solution where the checkboxes are simply set to disabled. 我更喜欢一个解决方案,其中复选框只是设置为禁用。

I'm looking for a C# solution but will be happy with a C#/Javascript solution. 我正在寻找一个C#解决方案,但会对C#/ Javascript解决方案感到满意。

Thanks! 谢谢!

Ok, found a fairly clean solution to this: 好的,找到了一个相当干净的解决方案:

in code-behind: 在代码隐藏中:

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);

in Javascript, scan all treeview nodes for those that have that className and disable the checkboxes associated to them: 在Javascript中,扫描所有树视图节点以查找具有该className的节点,并禁用与其关联的复选框:

    // 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];
}

Sadly, I don't have enough reputation to be able to comment directly on zukanta's answer which is a bit of a pain, but I had to make a modification in the javascript to make this work: 可悲的是,我没有足够的声誉能够直接评论zukanta的答案,这有点痛苦,但我不得不在javascript中进行修改以使其工作:

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

ie replace textSpan.firstChild.ClassName with textSpan.ClassName 即用textSpan.ClassName替换textSpan.firstChild.ClassName

Also worth pointing out that the JavaScript will error out unless all of your tree nodes in the treeview that you are addressing have a 另外值得指出的是,除非您正在寻址的树视图中的所有树节点都有一个,否则JavaScript将会出错

<span></span> 

in them. 在他们中。 You get a null reference at 你得到一个空引用

if (textSpan.firstChild) 

and no subsequent nodes are processed. 并且不处理后续节点。

I got around this point by adding a span with class=enabledTreeviewNode to all tree nodes that I didn't want disabled. 我通过向所有我不想禁用的树节点添加带有class = enabledTreeviewNode的span来解决这个问题。

You could also handle the exception in the JavaScript, I guess. 我猜你也可以在JavaScript中处理异常。

Hope this helps someone who stumbles across this (otherwise excellent) solution later on. 希望这有助于后来偶然发现这个(非常好的)解决方案的人。

You could use security trimming to not show items that the user doesn't have access to. 您可以使用安全修整来不显示用户无权访问的项目。 I don't know of any way to have the items displayed but not active. 我不知道有任何方式显示项目但不活动。 Disabling checkboxes on the client side only could create a security hole. 仅在客户端禁用复选框可能会创建安全漏洞。

Walkthrough: Filtering Site-Map Nodes Based on Security Roles 演练:根据安全角色过滤站点地图节点

ASP.NET Site-Map Security Trimming ASP.NET站点地图安全修整

The OP was looking for conditional disable but I just want to use the TreeView to display historical audit data, logs of when items were switched on. OP正在寻找条件禁用,但我只想使用TreeView来显示历史审计数据,以及项目何时开启的日志。 All the checkboxes on my page should be disabled. 应禁用我页面上的所有复选框。 It took me some time to find this elegant jQuery solution. 我花了一些时间来找到这个优雅的jQuery解决方案。 I hope it helps anyone with a similar issue. 我希望它可以帮助任何有类似问题的人。

Add the code below to your script section. 将以下代码添加到脚本部分。 As all input boxes will be disabled, there's no need to make any changes at all to your codebehind. 由于所有输入框都将被禁用,因此无需对代码隐藏进行任何更改。

<script type="text/javascript">
    $(document).ready(function () {
        $("input:checkbox").each(function () {
            $(this).prop('disabled', true);
        });
    });
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM