简体   繁体   中英

Alter an HTML table in code

I have a table with a specific layout. It starts like this:

<table>
    <tr bgcolor="#007ACC" style="color:White">
        <td width="145"><asp:Label Text="" ID="lblLevel" runat="server" /></td><td width="80"></td><td width="30"></td><td width="145"><asp:Label Text="" ID="lblGroupNumber" runat="server" /></td><td width="60"></td><td width="10">Active</td>
    </tr>
    <tr>
        <td colspan="5"> 
            <asp:TextBox ID="txtName" runat="server" width="460px"></asp:TextBox>
        </td>
        <td> 
            <asp:DropDownList ID="cboActive" runat="server" Width="50px">
                <asp:ListItem>Y</asp:ListItem>
                <asp:ListItem>N</asp:ListItem>
            </asp:DropDownList>
        </td>
    </tr>

The problem is, under certain conditions I need it to look like this:

<table>
    <tr bgcolor="#007ACC" style="color:White">
        <td width="145"><asp:Label Text="" ID="lblLevel" runat="server" /></td><td width="80"></td><td width="30"></td><td width="145"><asp:Label Text="" ID="lblGroupNumber" runat="server" /></td><td width="60"></td><td width="10">Active</td>
    </tr>
    <tr>
        <td colspan="2">
            <asp:TextBox ID="txtName" runat="server" width="460px"></asp:TextBox>
        </td>
        <td></td>
        <td colspan="2"> 
            <asp:TextBox ID="txtNumber" runat="server" width="460px"></asp:TextBox>
        </td>
        <td> 
            <asp:DropDownList ID="cboActive" runat="server" Width="50px">
                <asp:ListItem>Y</asp:ListItem>
                <asp:ListItem>N</asp:ListItem>
            </asp:DropDownList>
        </td>
    </tr>

I've hidden textboxes before, that's no problem. But the only way I can think of to do this is to hide td's using code. I've seen this:

How to hide columns in HTML table?

but they never explain how you can determine which td is to be hidden.

So, can this be done in code (preferably code-behind in C#)? If so, how?

In asp.net most elements can be programmatically handled as server controls with runat set:

<td colspan="2" runat="server" id="tdToHide"> 
      <asp:TextBox ID="txtNumber" runat="server" width="460px"></asp:TextBox>
</td>

In C#:

tdToHide.Visible = false;

This is one of many, many approaches to 'hide things' on a web page.

Another one would be a conditional CSS class on the td . The display of which would then be dealt with by styles on the page.

You can use LiteralControl and then based on condition change the content or AddControl to the literalcontrol . This can be performed from code behind

Other way to select specific HTML Tags can bei done with css Selectors. http://www.w3schools.com/cssref/css_selectors.asp Look at etc.: p:nth-child(2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM