tishantha
tishantha

Reputation: 497

GET ALL TAGS FROM HtmlGenericControl IN C#

I wanna get all html tags in a HTML control in C#. Here is my HTML code..

<div id="nav" runat="server" style="position: absolute;">
  <ul id="navList">
    <li runat="server" id="HOME">
      <a id="a1" runat="server" href="#">HOME</a>
      <!-- This is the sub nav -->
      <ul class="listTab">
        <li runat="server" id="HM1"><a id="a2" runat="server" href="#">About This Template</a></li>
        <li runat="server" id="HM2"><a id="a3" runat="server" href="#">Flash</a></li>
        <li runat="server" id="HM3"><a id="a4" runat="server" href="#">jQuery</a></li>
      </ul>
    </li>
    <li runat="server" id="BLOG">
      <a id="a5" runat="server" href="#">BLOG</a>
      <!-- This is the sub nav -->
      <ul class="listTab">
        <li runat="server" id="BM1"><a id="a6" runat="server" href="#">MENU 1</a></li>
        <li runat="server" id="BM2"><a id="a7" runat="server" href="#">MENU 2</a></li>
        <li runat="server" id="BM3">
          <a id="a8" runat="server" href="#">MENU 3</a>
          <ul class="listTab">
            <li runat="server" id="BM3S1"><a id="a9" runat="server" href="#">MENU 3 SUB 1</a></li>
            <li runat="server" id="BM3S2"><a id="a10" runat="server" href="#">MENU 3 SUB 2</a></li>
            <li runat="server" id="BM3S3"><a id="a11" runat="server" href="#">MENU 3 SUB 3</a></li>
          </ul>
        </li>
      </ul>
    </li>
    <li runat="server" id="ABOUT">
      <a id="a12" runat="server" href="#">About</a>
      <!-- This is the sub nav -->
      <ul class="listTab">
        <li runat="server" id="AM1"><a id="a13" runat="server" href="#">ABOUT MENU 1</a></li>
        <li runat="server" id="AM2"><a id="a14" runat="server" href="#">ABOUT MENU 2</a></li>
        <li runat="server" id="AM3"><a id="a15" runat="server" href="#">ABOUT MENU 3</a></li>
      </ul>
    </li>
    <li runat="server" id="PORTFIL"><a id="a16" runat="server" href="#">Porfolio</a></li>
    <li runat="server" id="CONTACT"><a id="a17" runat="server" href="#">Contact</a></li>
  </ul>
</div>

How to get a tags from above HTML code. Here is my method, it gives only "div" tags and "li" tags. What is wrong with my code. Please help..

private void LoadMenues(HtmlGenericControl subMenu) 
{
    foreach (HtmlGenericControl c in subMenu.Controls.OfType<HtmlGenericControl>()) 
    {
        string id = c.ID;
        string x = c.TagName; // <----** 
        MenuModel m = new MenuModel();
        m.vcMenuID = id;
        MenuModel menu = objCommon.MenuLookup(m).FirstOrDefault();

        if (menu == null) 
        {
            menu = new MenuModel();
            menu.vcMenuID = id;
            menu.intParentMenuID = pid;
            menu.bIsActiveMenu = 1;
            pid = objCommon.InsertMenu(menu);
        } 
        else 
        {
            pid = menu.intMenuID;
        }
        LoadMenues(c);
    }
    if (pid != null)
        pid = objCommon.GetMinPID(subMenu.ID);
}

Upvotes: 1

Views: 653

Answers (1)

Paolo Tedesco
Paolo Tedesco

Reputation: 57202

There doesn't seem to be anything wrong with your code, and you are calling the function recursively.

Please note, however, that you will only get controls having the runat="server" attribute defined with this code.

The controls you would expect to get and are not getting are those that don't have it?

Upvotes: 1

Related Questions