循环获取div的id

发布于 2024-12-25 17:56:54 字数 626 浏览 0 评论 0原文

在我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏日浅笑〃 2025-01-01 17:56:54

我编写了以下实用方法,您可以将其放入“实用工具带”类或其他类中。您可以像这样使用它: mainDiv.FindControlsOfType() 返回控件列表。

    /// <summary>
    /// Given a root Control, do a recursive search for Controls implementing or deriving from class/interface T
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="ctlRoot"></param>
    /// <returns></returns>
    public static List<T> FindControlsOfType<T>(this Control ctlRoot)
    {
        List<T> controlsFound = new List<T>();

        if (typeof(T).IsInstanceOfType(ctlRoot))
            controlsFound.Add((T)(object)ctlRoot);

        foreach (Control ctlTemp in ctlRoot.Controls)
        {
            controlsFound.AddRange(FindControlsOfType<T>(ctlTemp));
        }

        return controlsFound;
    }

您遇到的问题是 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.

    /// <summary>
    /// Given a root Control, do a recursive search for Controls implementing or deriving from class/interface T
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="ctlRoot"></param>
    /// <returns></returns>
    public static List<T> FindControlsOfType<T>(this Control ctlRoot)
    {
        List<T> controlsFound = new List<T>();

        if (typeof(T).IsInstanceOfType(ctlRoot))
            controlsFound.Add((T)(object)ctlRoot);

        foreach (Control ctlTemp in ctlRoot.Controls)
        {
            controlsFound.AddRange(FindControlsOfType<T>(ctlTemp));
        }

        return controlsFound;
    }

A problem you have is that div does not correspond to any standard ASP.NET web control, so HtmlGenericControl has to be used in the search. Therefore the search will pick up ANY HtmlGenericControl, not just div elements. Consequently, you will have to look at the TagName property of each control to ensure it's definitely a div.

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 new div order.

第几種人 2025-01-01 17:56:54

如果你只想要 div id:

    foreach (Control c in mainDiv.Controls)
    {
        if (c is HtmlGenericControl)
        {
            String id = c.ID;
        }
    }

If you just want div ids:

    foreach (Control c in mainDiv.Controls)
    {
        if (c is HtmlGenericControl)
        {
            String id = c.ID;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文