views:

496

answers:

4

Hi, I am trying to access an HTML table from code behind, and set its visible="false" property (depending on what value the user has selected). The table has an id value and a runat=server attribute.

How can I call the table from the code behind in C# 2008 and set its display?

+1  A: 

Make sure you have your table set up to run at sever.

Example

<table id="tblMyTable" runat="server">
....
</table>

On server side you can access it by using the variable tblMyTable

To hide the visibility is not simple. There is not a property for it since it is a Html control rather than a server control.

I would wrap the table in an ASP.NET control such as a panel, and hide the panel.

David Basarab
is a panel just a container?
user279521
yes, it does not have any rendering.
Bob Fincheimer
+2  A: 

I would wrap the table in an <asp:Panel control and change the visible property on that instead.

Joel Coehoorn
+1  A: 

Seting the visibility from the codebehind is a simple as setting the Visible property:

table_control.Visible = false;

If you are doing this in response to some client side activity, then you need some javascript:

document.getElementById("<%= table_control,ClientID %>").style.display = "none";

or jQuery:

$("#<%= table_control,ClientID %>").hide();

Call this from an onclick or onchange event, as needed for your page.

Ray
A: 

You should use an <asp:Table> control if you want to access the table from code behind eg

<asp:Table ID="Table1" CssClass="data" runat="server" CellSpacing="0">
    <asp:TableHeaderRow>
        <asp:TableHeaderCell>SKU</asp:TableHeaderCell>
        <asp:TableHeaderCell>Description</asp:TableHeaderCell>
        <asp:TableHeaderCell>Quantity</asp:TableHeaderCell>
        <asp:TableHeaderCell>Amount</asp:TableHeaderCell> 
    </asp:TableHeaderRow>
</asp:Table>

Bind data to the table eg. like so:

var row = new TableRow();

row.AddCell(stock.Sku);
row.AddCell(stock.Description);
row.AddCellTextbox("txtQty", cart.Values[key]);
row.AddCell(stock.Price.ToString());

Table1.Rows.Add(row);

Note: The table control doesnt provide viewstate for items added in code, for that you need to use a GridView or similar control.

James Westgate