C# 在展开/折叠时禁用 TreeView 节点上的回发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/749265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disabling postbacks on TreeView nodes on expand/collapse
提问by sarsnake
how to?
如何?
I tried AutoPostback=false
, but clicking on the node still posts the page back. Any ideas?
我试过了AutoPostback=false
,但点击节点仍然返回页面。有任何想法吗?
回答by Zhaph - Ben Duguid
The default setting for EnableClientScriptis true, so by default, expanding and collapsing of nodes should be happening client side. Note the remarks for that property though (Emphasis mine):
EnableClientScript的默认设置为true,因此默认情况下,节点的展开和折叠应该发生在客户端。请注意该属性的备注(重点是我的):
Use the EnableClientScript property to specify whether the TreeView control renders client-side script on compatible browsers to handle expanding and collapsing events. When this property is set to true, compatible browsers execute the code to expand and collapse nodes on the client. The tree node data must be known in advance for the client-side script to work. Using client script prevents a post back to the server each time a node is expanded or collapsed.
使用 EnableClientScript 属性指定 TreeView 控件是否在兼容浏览器上呈现客户端脚本以处理展开和折叠事件。当此属性设置为 true 时,兼容浏览器将执行代码以展开和折叠客户端上的节点。必须事先知道树节点数据,客户端脚本才能工作。每次展开或折叠节点时,使用客户端脚本可防止回发到服务器。
Things to check:
检查事项:
- You do have the node data avaiable to the tree view data source without a postback
- You haven't set EnableClientScript to false
- You're not using a control adaptor that's overriding this behaviour
- You aren't somehow confusing the server into thinking that you're a downlevel browser(that page is running on .NET 1.1 I think, so might be different to your server - it treats my version of IE with an overweight UserAgent string as "Downlevel" because the check cuts off after 128 characters, and fails)
- 您确实拥有可用于树视图数据源的节点数据,而无需回发
- 您尚未将 EnableClientScript 设置为 false
- 您没有使用覆盖此行为的控制适配器
- 您不会以某种方式让服务器误以为您是下层浏览器(我认为该页面在 .NET 1.1 上运行,因此可能与您的服务器不同 - 它使用超重的 UserAgent 字符串将我的 IE 版本视为“ Downlevel”,因为检查在 128 个字符后切断,并且失败)
If you can't have all the data available, then you should ensure that you are setting PopulateOnDemand, PopulateNodesFromClientand TreeNodePopulatecorrectly.
如果您无法获得所有数据,则应确保正确设置PopulateOnDemand、PopulateNodesFromClient和TreeNodePopulate。
Response to comments
回复评论
Yes, there's no reason why you can't do what you're attempting to do with a tree view control - indeed, it should do most of this for you - unless you aren't supplying the whole tree up front, in which case you'll need to supply the methods that will populate the nodes through PopulateNodesFromClient and TreeNodePopulate, assuming the browser is recognised correctly (those previous links have examples of both with and without postbacks).
是的,没有理由你不能用树视图控件做你试图做的事情 - 事实上,它应该为你做大部分 - 除非你没有预先提供整棵树,在这种情况下您需要提供将通过 PopulateNodesFromClient 和 TreeNodePopulate 填充节点的方法,假设浏览器被正确识别(前面的链接有回发和不回发的示例)。
The jQuery Tree View plugins will probably be more flexible (I've not used them so I wouldn't know - I have used the TreeView control we're discussing, but was getting the features "for free" because I only had a small tree, and was supplying all the data up front), but will require a bit more work to set up:
jQuery Tree View 插件可能会更灵活(我没有使用过它们,所以我不知道 - 我已经使用了我们正在讨论的 TreeView 控件,但是“免费”获得了这些功能,因为我只有一个小的树,并预先提供所有数据),但需要更多的工作来设置:
- You'll need to pull in the jQuery framework, if you're not already using it
- Write a web service that it can call to get the initial data along with subsequent nodes.
- Potentially worry about what happens to browsers with limited or no JavaScript support.
- 如果您还没有使用它,则需要引入 jQuery 框架
- 编写一个 Web 服务,它可以调用它来获取初始数据和后续节点。
- 可能会担心 JavaScript 支持有限或没有 JavaScript 支持的浏览器会发生什么。
3 may or may not be the kicker for you - in everything we do, we try to use JavaScript to enhance functionality, not provide the functionality - as the jQuery library is all client side, if the browser doesn't support JavaScript, your jQuery TreeView control won't appear, and you'll possibly be losing a key navigational element, whereas a Server control like the ASP.NET TreeView control will notice that JavaScript isn't supported and resolve this for you by falling back to a server based (i.e. post-back) version of the control.
3 可能适合您,也可能不适合您 - 在我们所做的一切中,我们尝试使用 JavaScript 来增强功能,而不是提供功能 - 因为 jQuery 库都是客户端,如果浏览器不支持 JavaScript,您的 jQuery TreeView 控件不会出现,您可能会丢失一个关键的导航元素,而像 ASP.NET TreeView 控件这样的服务器控件会注意到不支持 JavaScript 并通过回退到基于服务器的方式为您解决此问题(即回发)版本的控件。
You should also remember that jQuery isn't a Microsoft thing (although they do now supportit), and has been around for quite some time, and as a client side offering, it works independantly of the server technology - MS give us a number of "server" controls within the ASP.NET framework that we canuse if we want to, and doing so can make our lives easier (usually in the short term anyway), but we don't have to - a prime example is the recently released ASP.NET MVC framework, which is a different way of building web applications on IIS using the .NET stack - but generally without the server controls.
你还应该记住 jQuery 不是微软的东西(尽管他们现在支持它),并且已经存在了很长一段时间,作为客户端产品,它独立于服务器技术工作 - MS 给我们一个数字如果需要,我们可以使用ASP.NET 框架中的“服务器”控件,这样做可以使我们的生活更轻松(无论如何通常在短期内),但我们不必 - 一个主要的例子是最近发布的 ASP.NET MVC 框架,这是一种使用 .NET 堆栈在 IIS 上构建 Web 应用程序的不同方式 - 但通常没有服务器控件。
回答by mas_oz2k1
The asp.net treeview does not support this functionality,
asp.net 树视图不支持此功能,
try obout treeview:
It does load the nodes without reloading the whole tree.
它会在不重新加载整个树的情况下加载节点。
回答by Adam
A way forward.....
前进的道路......
The automatic select action for each node is SelectExpand, the "Select" part of this enum is interpreted as "I've selected something and need to postback". To just "Expand" the node without causing a postback:
每个节点的自动选择操作是 SelectExpand,这个枚举的“选择”部分被解释为“我选择了一些东西并且需要回发”。要在不导致回发的情况下“扩展”节点:
TreeView1.Nodes(0).SelectAction = TreeNodeSelectAction.Expand
TreeView1.Nodes(0).SelectAction = TreeNodeSelectAction.Expand
回答by user2626412
add below code in a css file. Then select TreeView property callled NodeStyle-> CssClass=disabled
在css文件中添加以下代码。然后选择名为 NodeStyle-> CssClass=disabled 的 TreeView 属性
a.disabled {
pointer-events: none;
cursor: default;
}
a.disabled {
pointer-events: none;
cursor: default;
}