简体   繁体   English

修改代码中的HTML表

[英]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. 但是我能想到的唯一方法是隐藏td's使用代码。 I've seen this: 我已经看到了:

How to hide columns in HTML table? 如何隐藏HTML表中的列?

but they never explain how you can determine which td is to be hidden. 但是他们从不解释如何确定要隐藏哪个td

So, can this be done in code (preferably code-behind in C#)? 那么,这可以用代码(最好是C#中的代码隐藏)完成吗? If so, how? 如果是这样,怎么办?

In asp.net most elements can be programmatically handled as server controls with runat set: 在asp.net中,可以将大多数元素以编程方式设置为带有runat服务器控件:

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

In C#: 在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 . 另一个可能是td上的条件CSS类。 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 . 您可以使用LiteralControl,然后根据条件更改内容或将Control添加literalcontrol This can be performed from code behind 这可以从后面的代码中执行

Other way to select specific HTML Tags can bei done with css Selectors. 选择特定HTML标记的其他方法可以通过CSS选择器来完成。 http://www.w3schools.com/cssref/css_selectors.asp Look at etc.: p:nth-child(2) http://www.w3schools.com/cssref/css_selectors.asp看看等等:p:nth-​​child(2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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