循环获取div的id
在我的asp.net页面中,当单击按钮(getDivsOrder)时,我想使用循环或类似的东西按顺序获取mainDiv中div的id。我怎样才能做到这一点?
<ContentTemplate>
<div id="mainDiv" runat="server">
<div id="Red" runat="server"></div>
<div id="Yellow" runat="server"></div>
<div id="Green" runat="server"></div>
...
</div>
<asp:Button ID="getDivsOrder" runat="server" Text="Update label "
onclick="btnHelloWorld_Click" />
</ContentTemplate>
in my asp.net page when the button(getDivsOrder) is clicked, I want to get the ids of the divs in the mainDiv by order using a loop or something like that . How can I do that?
<ContentTemplate>
<div id="mainDiv" runat="server">
<div id="Red" runat="server"></div>
<div id="Yellow" runat="server"></div>
<div id="Green" runat="server"></div>
...
</div>
<asp:Button ID="getDivsOrder" runat="server" Text="Update label "
onclick="btnHelloWorld_Click" />
</ContentTemplate>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我编写了以下实用方法,您可以将其放入“实用工具带”类或其他类中。您可以像这样使用它:
mainDiv.FindControlsOfType()
返回控件列表。您遇到的问题是
div
不对应于任何标准 ASP.NET Web 控件,因此必须在搜索中使用HtmlGenericControl
。因此,搜索将选取任何HtmlGenericControl
,而不仅仅是div
元素。因此,您必须查看每个控件的TagName
属性,以确保它确实是一个div
。另外,请注意,在服务器端执行此操作意味着您仅检查服务器上
mainDiv
控件的内容和顺序。如果您要在客户端更改 div 的顺序(例如使用 jQuery Draggable),服务器将不会知道这一点,除非您使用额外的 javascript 回发新的div
顺序。I wrote the following utility method which you can put into a 'Utility Toolbelt' class or whatever. You would use it like:
mainDiv.FindControlsOfType<HtmlGenericControl>()
to return a list of controls.A problem you have is that
div
does not correspond to any standard ASP.NET web control, soHtmlGenericControl
has to be used in the search. Therefore the search will pick up ANYHtmlGenericControl
, not justdiv
elements. Consequently, you will have to look at theTagName
property of each control to ensure it's definitely adiv
.Also, be aware that doing this at the server side will mean you are only checking the contents and order of the
mainDiv
controls on the server. If you are changing the order of the divs on the client side e.g. using jQuery draggable, the server won't know about it unless you use additional javascript to post back the newdiv
order.如果你只想要 div id:
If you just want div ids: