Pregunta

Is it possible to reorder HtmlGenericControls that are statically put on a page? For example I have a menu that has a container element for each section. I want to reorder the position of the sections.

<!-- hard coded in master page frontend -->

    <!-- In Masterpage frontend -->
    <div runat="server" id="menuItem1">    
    </div>

    <div runat="server" id="menuItem2">    
    </div>

    <div runat="server" id="menuItem3">    
    </div>

Is there some way to reorder the display of the top three controls...

I know this is weird, so please try and stay focused on the question

¿Fue útil?

Solución

You can grab a reference to the parent control, and re-add the controls in the order you want:

var parent = menuItem1.Parent;
parent.Controls.Add(menuItem2);
parent.Controls.Add(menuItem1);
parent.Controls.Add(menuItem3);

In this example, the divs will be reordered to menuItem2, menuItem1, menuItem3. Note that this will also move them to the end if there are other controls already in the same container. If you want more control over the placement, you can use .Controls.AddAt to add at a specified index.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top