[英]Disabling ASP.net treeview checkboxes
你們如何有條件地禁用asp樹視圖中的復選框?
例如,如果應用程序用戶沒有特定權限,請在權限樹視圖中禁用該權限條目復選框。
這是我正在尋找的,這是winform應用程序中的等價物(禁用復選框,文本顯示為灰色):
我看到了其他解決方案,其中復選框上的click事件被截獲並被忽略。 我更喜歡一個解決方案,其中復選框只是設置為禁用。
我正在尋找一個C#解決方案,但會對C#/ Javascript解決方案感到滿意。
謝謝!
好的,找到了一個相當干凈的解決方案:
在代碼隱藏中:
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];
}
可悲的是,我沒有足夠的聲譽能夠直接評論zukanta的答案,這有點痛苦,但我不得不在javascript中進行修改以使其工作:
if (textSpan.firstChild)
if (textSpan.className == "disabledTreeviewNode")
childCheckBoxes[i].disabled = true;
即用textSpan.ClassName替換textSpan.firstChild.ClassName
另外值得指出的是,除非您正在尋址的樹視圖中的所有樹節點都有一個,否則JavaScript將會出錯
<span></span>
在他們中。 你得到一個空引用
if (textSpan.firstChild)
並且不處理后續節點。
我通過向所有我不想禁用的樹節點添加帶有class = enabledTreeviewNode的span來解決這個問題。
我猜你也可以在JavaScript中處理異常。
希望這有助於后來偶然發現這個(非常好的)解決方案的人。
OP正在尋找條件禁用,但我只想使用TreeView來顯示歷史審計數據,以及項目何時開啟的日志。 應禁用我頁面上的所有復選框。 我花了一些時間來找到這個優雅的jQuery解決方案。 我希望它可以幫助任何有類似問題的人。
將以下代碼添加到腳本部分。 由於所有輸入框都將被禁用,因此無需對代碼隱藏進行任何更改。
<script type="text/javascript">
$(document).ready(function () {
$("input:checkbox").each(function () {
$(this).prop('disabled', true);
});
});
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.