betteroutthanin
betteroutthanin

Reputation: 7546

C# NotSupportedException when adding InnerHTML into a HTML table

In my .aspx file I have the table below:

 <table id="table1" style="width: 100%;" runat="server"></table>

I want to access this table and insert html into this table in C#(.aspx.cs),I tried this:

 HtmlTable table = (HtmlTable)(form1.FindControl("table1"));
 table.InnerHtml = "<tr><td></td></tr>";

But I got NotSupportedException. How can I solve this?

Upvotes: 0

Views: 1902

Answers (3)

dckuehn
dckuehn

Reputation: 2475

If you want to be able to retrieve the table in the code-behind, you need to use a .Net control and declare your table using

<asp:Table runat="server" ... />
     ...
</table>

Upvotes: 1

Laird Streak
Laird Streak

Reputation: 399

If the control does not have a runat="server" your code behind will not be able to access the object at runtime. :)

Upvotes: 4

Morten Anderson
Morten Anderson

Reputation: 2321

use runat server to make the table available serverside

<table runat="server" id="table1" style="width: 100%;"></table>

For reference you could take a look at this SO question - Why does ASP.NET webforms need the Runat=“Server” attribute?

Upvotes: 3

Related Questions