0

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?

betteroutthanin
  • 7,148
  • 8
  • 29
  • 48
  • Well, the documentation does indeed say you can't use InnerHtml: http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmltable.innerhtml(v=vs.110).aspx – Will Eddins May 07 '14 at 20:53
  • Are you trying to add rows to the table? If so you should use HtmlTable.Rows.Add and the newly created row. Have a look here for some sample code: http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmltable.rows(v=vs.110).aspx – Laird Streak May 07 '14 at 20:55

3 Answers3

4

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

Laird Streak
  • 397
  • 5
  • 8
3

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?

Community
  • 1
  • 1
Morten Anderson
  • 2,301
  • 15
  • 20
1

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>
dckuehn
  • 2,427
  • 3
  • 27
  • 37