Stewie Griffin
Stewie Griffin

Reputation: 9327

how can I change order of <tr> tags in the table C#?

If I have a table in .aspx

<table id="table" >
  <tr id="first" runat="server"> blablabla>
       <td></td>
   </tr>
   <tr id="second" runat="server">
       <td></td>
   </tr>
</table>

How can I change order from second to first in the code behind?

I tried to wrap tr with placeholders and hide/display them accordingly, but it doesn't allow me to do that as I get duplicate IDs inside these rows. And I can't use javascript for that..

Upvotes: 0

Views: 1061

Answers (2)

Oded
Oded

Reputation: 499212

As you have not set either tr or table as server side code (by using a runat="server" attribute), they are not visible to the code behind and it cannot change the ordering.

In aspx:

<table id="table" runat="server">
    <tr id="first" runat="server">
        <td>blablabla</td>
    </tr>
    <tr id="second" runat="server">
        <td>&nbsp;</td>
    </tr>
</table>

In code behind:

var row = table.Rows[0]; // get reference to first row
table.Rows.Remove(row);  // remove it
table.Rows.Add(row);     // Add again, at the end (default)

Upvotes: 3

Justin Niessner
Justin Niessner

Reputation: 245479

Since this is static markup rather than a server side control (none of the tags are set to runat="server"), the server side C# code can't modify them.

Upvotes: 2

Related Questions