有没有办法找到从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和下一个兄弟控制
发布评论