Question

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?

Was it helpful?

Solution 2

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>

OTHER TIPS

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.

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

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.

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

Remove the below elements

<thead>
</thead>
<tbody>
</tbody>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top