2013-02-25 115 views
2

你們如何有條件地禁用asp樹視圖中的複選框?禁用ASP.net treeview複選框

例如,如果應用程序用戶沒有特定權限,請在權限樹視圖中禁用該權限條目複選框。

這裏就是我要找的,這是一個winform應用程序的equivaqlent(其中文本是灰色的複選框被禁用):

enter image description here

我看到其他的解決方案,其中click事件在複選框被攔截並忽略。我寧願選擇複選框設置爲禁用的解決方案。

我正在尋找C#解決方案,但會很滿意C#/ Javascript解決方案。

謝謝!

回答

2

好了,找到了一個還算乾淨地解決了這個:

代碼隱藏

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

,掃描所有樹狀節點的那些擁有該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]; 
} 
+0

這工作得很好,但我有一個非常奇怪的問題。我的樹視圖在回發中保持不變,但我必須運行這個JS 2x才能使用它,除非我將if(textSpan.firstChild.className ==「disabledTreeviewNode」)'行更改爲'if(textSpan.className = =「disabledTreeviewNode」|| textSpan.firstChild.className ==「disabledTreeviewNode」)'在第一次調用時,'textSpan.className ==「disabledTreeviewNode」'爲'true',隨後調用您的代碼工作於'textSpan.firstChild.className ==「disabledTreeviewNode」'。不是100%肯定我明白,但檢查這兩個條件使這項工作。 – atconway 2013-09-17 19:50:08

+0

作爲一個後續,看看這篇文章,我做了所有'JavaScript'約3行'jQuery':http://stackoverflow.com/questions/18858842/convert-scanning-for-classname- JavaScript的塊使用的-jquery的 – atconway 2013-09-17 20:22:00

0

您可以使用安全修整來不顯示用戶無權訪問的項目。我不知道有什麼方法讓項目顯示但不活躍。僅在客戶端禁用複選框可能會造成安全漏洞。

Walkthrough: Filtering Site-Map Nodes Based on Security Roles

ASP.NET Site-Map Security Trimming

+0

感謝DaveB,但權限treeview只是一個例子。我只是想在某些情況下在asp.net樹視圖中將一些複選框設置爲「禁用」。我還需要顯示那些樹視圖節點,但只需將它們設置爲禁用。所以安全修整看起來像是一個很遙不可及的解決方案,但無論如何感謝。 – zukanta 2013-02-26 00:32:47

1

可悲的是,我沒有足夠的聲譽能夠直接在zukanta的回答這是一個有點痛發表評論,但我不得不做出修改中的JavaScript,使這項工作:

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

如更換textSpan.firstChild.ClassNametextSpan.ClassName

另外值得指出的是JavaScript的錯誤,除非所有在TreeView的樹節點,你正在處理有

<span></span> 

其中。您在

if (textSpan.firstChild) 

中得到空引用,並且不會處理後續節點。

我通過向所有不想禁用的樹節點添加了class = enabledTreeviewNode的span來解決這個問題。

我想你也可以在JavaScript中處理異常。

希望這可以幫助那些稍後絆倒這個(否則優秀)解決方案的人。