寻找previous和下一个兄弟控制

编程入门 行业动态 更新时间:2024-10-23 15:26:36
本文介绍了寻找previous和下一个兄弟控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有没有办法找到从code-背后的ASP形式previous和下一个兄弟的控制,类似的FindControl()?

Is there a way to find the previous and next sibling controls in an ASP form from code-behind, similar to findControl()?

有时候你不想一个ID分配到控制,所以您可以为了找到它做父母()的FindControl(ID)。我累了未来与ID的时候我所能做的就是previousControl()或东西(一拉jQuery的)。

Sometimes you don't want to assign an ID to a control just so you can do a parent().findControl("ID") in order to find it. I'm tired of coming up with IDs when all I could do is previousControl() or something (a la jQuery).

这也将是在你为了解决其中有一个类似的布局,并且不希望一个解决逐一几个控件编写一个通用函数情况下非常有用。

This would also be useful in situations where you write a general function in order to address several controls which have a similar layout and don't want to address them one by one.

感谢您的任何建议。

推荐答案

有关后人,这里是我结束了写作的功能。工作得非常好(在实际项目中进行测试):

For posterity, here is the function I ended up writing. Works very well (tested in a real project):

public static Control PreviousControl(this Control control) { ControlCollection siblings = control.Parent.Controls; for (int i = siblings.IndexOf(control) - 1; i >= 0; i--) { if (siblings[i].GetType() != typeof(LiteralControl) && siblings[i].GetType().BaseType != typeof(LiteralControl)) { return siblings[i]; } } return null; }

要使用这样的:

Control panel = textBox.PreviousControl();

和下一控制:

public static Control NextControl(this Control control) { ControlCollection siblings = control.Parent.Controls; for (int i = siblings.IndexOf(control) + 1; i < siblings.Count; i++) { if (siblings[i].GetType() != typeof(LiteralControl) && siblings[i].GetType().BaseType != typeof(LiteralControl)) { return siblings[i]; } } return null; }

此溶液的Atzoya的优点是,首先,你不需要原来的控制有一个ID,因为我做的基于实例的搜索。其次,你要知道,ASP,以使您的静态HTML在你的真实的对照组之间产生多个文字对照。这就是为什么我跳过它们,否则你将保持匹配的垃圾。当然,这样做的缺点是你无法找到一个控制,如果是文字。这种限制是不是在我的使用问题。

The advantage of this solution over that of Atzoya is that, first, you don't need the original control to have an ID since I do the search based on instance. Second, you have to know that ASP generates several Literal controls in order to render your static HTML in between your "real" controls. That's why I skip them, or you will keep matching junk. Of course the downside of this is you can't find a control if it's a Literal. This limitation was not a problem in my use.

更多推荐

寻找previous和下一个兄弟控制

本文发布于:2023-11-30 04:09:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648669.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:兄弟   previous

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!