Bat_Programmer
Bat_Programmer

Reputation: 6851

Error in table when adding runat=server

I have a html table in my ASPX page and would like to use it in code-behind for some processing. The table is shown as below:

<table class="hovertable" id="tblData">
    <tr>
        <th>ID:</th>
        <td colspan="3" style="font-weight: bold">
            <%= Eval("ID") %>
        </td>
    </tr>
    <tr>
        <th>Date:</th>
        <td><%# Eval("Date", "{0:dd-MMM-yyyy}") %></td>
        <th>Amount:</th>
        <td><%# Eval("Amount", "{0:C}") %>
    </tr>
</table>

However, when I add the runat="server" attribute to my table, I am produced with the following error:

CS1502: The best overloaded method match for 'System.Web.UI.HtmlControls.HtmlTableRowCollection.Add(System.Web.UI.HtmlControls.HtmlTableRow)' has some invalid arguments

Any ideas what may be wrong here? Am I missing out anything?

Upvotes: 3

Views: 18618

Answers (6)

Mr.X
Mr.X

Reputation: 31345

Remove the below elements

<thead>
</thead>
<tbody>
</tbody>

Upvotes: 0

Nikhil Mittal
Nikhil Mittal

Reputation: 1

Also if we remove the tbody element it will not throw error for td mismatch Nikhil Mittal

Upvotes: 0

Bat_Programmer
Bat_Programmer

Reputation: 6851

OK guys, I have solved this issue by myself. The problem causing it was because of a <td> not having the corresponding <tr> element. It was something like below:

<table class="hovertable" id="tblData">
    <tr>
        <th>ID:</th>
        <td colspan="3" style="font-weight: bold">
            <%= Eval("ID") %>
        </td>
    </tr>
    <tr>
        <th>Date:</th>
        <td><%# Eval("Date", "{0:dd-MMM-yyyy}") %></td>
        <th>Amount:</th>
        <td><%# Eval("Amount", "{0:C}") %>
    </tr>
    <td colspan='4'>
       Some data....
    </td>
</table>

Upvotes: 4

Rob Angelier
Rob Angelier

Reputation: 2333

An html table (which is not a pure asp.net server control) can't contain asp.net server controls. Take a look at this answer:

http://forums.asp.net/t/1524580.aspx/1

In my opinion, you should ask yourself the following question?

Do i need to solve this client or server side?

if your answer is client, you should implement the update logic with Ajax, otherwise you could use the ASP.NET server control and implement it server side.

Upvotes: 4

Conrad Lotz
Conrad Lotz

Reputation: 8838

Try adding <asp:Labels> where you need to manipulate data.

<table>
<tr><td><asp:Label id="lblRow" runat="server" /></td></tr>
</table>

Table columns and rows cannot be accessed via code behind if you have runat="server" in the tag because they are pure html.

Another way is to use a StringBuilder to create the html table in the code-behind and and asp:LiteralControl to output the table.

Upvotes: 0

Priyank Patel
Priyank Patel

Reputation: 7006

I think you can use this for the same purpose

<asp:Table ID="Table1" runat="server">
</asp:Table>

What you are trying to do is adding runat="server" attribute to a HTML control

Upvotes: 0

Related Questions