禁用ASP.net树视图复选框 [英] Disabling ASP.net treeview checkboxes
问题描述
您如何在asp树视图中有条件地禁用复选框?
例如,如果应用程序用户没有某个权限,请禁用该权限项一个权限树视图。
这是我正在寻找,这是winform应用程序的equivaqlent(复选框被禁用,其中文本是灰色的) p>
>
我看到了其他解决方案,其中复选框上的点击事件被拦截和忽略。我希望一个解决方案,其中的复选框只是设置为禁用。
我正在寻找一个C#解决方案,但会很高兴与C#/ Javascript解决方案。 p>
谢谢!
代码隐藏:
TreeNode newNode = new TreeNode permission.ToString());
newNode.SelectAction = TreeNodeSelectAction.None; // no Link
if(shouldDisableCheckbox)
{
//设置一个类,这样禁用的节点可以通过CSS
//格式化,并且可以标识为disabled Javascript。
newNode.Text =< span class = disabledTreeviewNode> + newNode.Text +< / span>;
}
nodes.Add(newNode); JavaScript中的
,扫描所有具有该className的树视图节点并禁用与其关联的复选框:
//通过在代码背后创建的启动脚本调用。
//禁用所有包含具有class = disabledTreeviewNode的文本的树视图复选框。
// treeviewID是treeView的ClientID
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;
}
}
}
函数GetCheckBoxTextSpan(checkBox)
{
//将标签文本设置为节点名
var parentDiv = checkBox.parentNode;
var nodeSpan = parentDiv.getElementsByTagName(span);
return nodeSpan [0];
}
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.
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):
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.
I'm looking for a C# solution but will be happy with a C#/Javascript solution.
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:
// 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树视图复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!