在 ASP.NET 中查找控件的更好方法
我有一个复杂的asp.net表单,在一个表单中甚至有50到60个字段,就像Multiview
一样,在MultiView内部我有一个GridView
,在GridView内部我有几个复选框
。
目前我正在使用 FindControl() 方法的链接并检索子 ID。
现在,我的问题是是否有其他方法/解决方案来查找 ASP.NET 中的嵌套控件。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果您正在寻找一种特定类型的控件,您可以使用像这样的递归循环 -
http://weblogs .asp.net/eporter/archive/2007/02/24/asp-net-findcontrol-recursive-with-generics.aspx
这是我制作的一个示例,它返回给定类型的所有控件
If you're looking for a specific type of control you could use a recursive loop like this one -
http://weblogs.asp.net/eporter/archive/2007/02/24/asp-net-findcontrol-recursive-with-generics.aspx
Here's an example I made that returns all controls of the given type
像往常一样迟到了。如果有人仍然对此感兴趣,那么有许多相关的SO问题和答案。我的递归扩展方法版本用于解决此问题:
Late as usual. If anyone is still interested in this there are a number of related SO questions and answers. My version of recursive extension method for resolving this:
所有突出显示的解决方案都使用递归(这会降低性能)。这是没有递归的更干净的方法:
All the highlighted solutions are using recursion (which is performance costly). Here is cleaner way without recursion:
FindControl 不会在嵌套控件中递归搜索。它只查找 NamigContainer 的控件是您调用 FindControl 的 Control。
默认情况下,ASP.Net 不会递归地查看嵌套控件,这是有原因的:
考虑到出于可重用性原因,您希望将 GridView、Formview、UserControls 等封装在其他 UserControl 中。如果您已经在页面中实现了所有逻辑并通过递归循环访问这些控件,那么重构将非常困难。如果您已经通过事件处理程序(例如 GridView 的 RowDataBound)实现了逻辑和访问方法,那么它会更简单并且更不容易出错。
FindControl does not search within nested controls recursively. It does only find controls that's NamigContainer is the Control on that you are calling FindControl.
Theres a reason that ASP.Net does not look into your nested controls recursively by default:
Consider you want to encapsulate your GridViews, Formviews, UserControls etc. inside of other UserControls for reusability reasons. If you would have implemented all logic in your page and accessed these controls with recursive loops, it'll very difficult to refactor that. If you have implemented your logic and access methods via the event-handlers(f.e. RowDataBound of GridView), it'll be much simpler and less error-prone.
https://blog.codinghorror.com/recursive-pagefindcontrol/
或
https://blog.codinghorror.com/recursive-pagefindcontrol/
OR
控件上的操作管理
在基类中创建以下类。
类 获取所有控件:
从数据库:
获取特定用户允许的数据集中 (DTActions) 动态的所有操作 ID(如 divAction1、divAction2 ....)。
在 Aspx 中:
在 HTML 中,将 Action(按钮、锚点等)放入 div 或 span 中,并为它们提供 id,如
IN CS:
在您的页面上使用此功能:
Action Management On Controls
Create below class in base class.
Class To get all controls:
From Database:
Get All Actions IDs (like divAction1,divAction2 ....) dynamic in DATASET (DTActions) allow on specific User.
In Aspx:
in HTML Put Action(button,anchor etc) in div or span and give them id like
IN CS:
Use this function on your page:
递归查找与指定谓词匹配的所有控件(不包括根控件):
用法:
Recursively find all controls matching the specified predicate (do not include root Control):
Usage:
我决定只构建控件字典。更难维护,可能比递归 FindControl() 运行得更快。
在我因为没有回答 OP 的问题而放弃之前...
问:现在,我的问题是有没有其他方法/解决方案可以在 ASP.NET 中找到嵌套控件?
答:是的,首先就不需要寻找它们。为什么要搜索你已经知道的东西呢?最好构建一个允许引用已知对象的系统。
I decided to just build controls dictionaries. Harder to maintain, might run faster than the recursive FindControl().
And before I get down-thumbs for not answering the OP's question...
Q: Now, my question is that is there any other way/solution to find the nested control in ASP.NET?
A: Yes, avoid the need to search for them in the first place. Why search for things you already know are there? Better to build a system allowing reference of known objects.
以下示例定义了 Button1_Click 事件处理程序。调用时,此处理程序使用 FindControl 方法来定位包含页面上 ID 属性为 TextBox2 的控件。如果找到该控件,则使用 Parent 属性确定其父控件,并将父控件的 ID 写入页面。如果未找到 TextBox2,则会将“Control Not Found”写入页面。
The following example defines a Button1_Click event handler. When invoked, this handler uses the FindControl method to locate a control with an ID property of TextBox2 on the containing page. If the control is found, its parent is determined using the Parent property and the parent control's ID is written to the page. If TextBox2 is not found, "Control Not Found" is written to the page.