将表(TD)中的数据和控件移位

时间:2012-08-01 22:21:09

标签: c# asp.net html

我试图弄清楚如何移动asp:xxx控制的一些单元格(而不是整行)。

下面是一个示例布局(使用文本),以显示数据的布局方式以及我希望它显示的内容。这可以在页面加载后面的代码中完成,因为条件将被设置为显示或隐藏2行。

基本上,我需要(有条件地)隐藏细胞XXX xxx YYY和yyy。转移它们。

Before and After

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .tableNoPad
        {
            padding: 0;
            margin: 0;
            border-style: none;
            width: 100%;
            border-collapse: collapse;
            empty-cells: hide;
        }
        .altrowStandard
        {
            background-color: lightblue;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    Before:
    <div>
        <table class="tableNoPad">
            <tr>
                <td>
                    AAA
                </td>
                <td>
                    aaa
                </td>
                <td>
                    &nbsp;
                </td>
                <td>
                    AAA
                </td>
                <td>
                    aaa
                </td>
            </tr>
            <tr class="altrowStandard">
                <td>
                    BBB
                </td>
                <td>
                    bbb
                </td>
                <td runat="server" id="hide1a">
                    &nbsp;
                </td>
                <td runat="server" id="hide1b">
                    BBB
                </td>
                <td runat="server" id="hide1c">
                    bbb
                </td>
            </tr>
            <tr>
                <td>
                    CCC
                </td>
                <td>
                    ccc
                </td>
                <td id="hide2a" runat="server">
                    &nbsp;
                </td>
                <td runat="server" id="hide2b">
                    XXX
                </td>
                <td runat="server" id="hide2c">
                    xxx
                </td>
            </tr>
            <tr class="altrowStandard">
                <td>
                    DDD
                </td>
                <td>
                    ddd
                </td>
                <td id="hide3a" runat="server">
                    &nbsp;
                </td>
                <td runat="server" id="hide3b">
                    YYY
                </td>
                <td runat="server" id="hide3c">
                    yyy
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                </td>
                <td>
                    &nbsp;
                </td>
                <td>
                    CCC
                </td>
                <td>
                    ccc
                </td>
            </tr>
            <tr class="altrowStandard">
                <td>
                </td>
                <td>
                </td>
                <td>
                    &nbsp;
                </td>
                <td>
                    DDD
                </td>
                <td>
                    ddd
                </td>
            </tr>
        </table>
    </div>
    After:
    <div>
        <table class="tableNoPad">
            <tr>
                <td>
                    AAA
                </td>
                <td>
                    aaa
                </td>
                <td>
                    &nbsp;
                </td>
                <td>
                    AAA
                </td>
                <td>
                    aaa
                </td>
            </tr>
            <tr class="altrowStandard">
                <td>
                    BBB
                </td>
                <td>
                    bbb
                </td>
                <td runat="server" id="Td1">
                    &nbsp;
                </td>
                <td runat="server" id="Td2">
                    BBB
                </td>
                <td runat="server" id="Td3">
                    bbb
                </td>
            </tr>
            <tr>
                <td>
                    CCC
                </td>
                <td>
                    ccc
                </td>
                <td id="Td4" runat="server">
                    &nbsp;
                </td>
                <td runat="server" id="Td5">
                    CCC
                </td>
                <td runat="server" id="Td6">
                    ccc
                </td>
            </tr>
            <tr class="altrowStandard">
                <td>
                    DDD
                </td>
                <td>
                    ddd
                </td>
                <td id="Td7" runat="server">
                    &nbsp;
                </td>
                <td runat="server" id="Td8">
                    DDD
                </td>
                <td runat="server" id="Td9">
                    ddd
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

一个选项可能是在您的代码中构建DataTable,然后将其绑定到GridView。将GridView添加到页面类似于以下内容:

<form id="form1" runat="server">
    <asp:GridView runat="server" ID="ExampleGridView" 
                AutoGenerateColumns="true" ShowHeader="false" />
</form>

然后在页面加载中,您可以根据需要构建DataTable,例如:

protected void Page_Load(object sender, EventArgs e)
{
    DataTable table = new DataTable();
    var condition = false;

    for (int i = 0; i < 4; i++) table.Columns.Add();

    AddArray(table, new string[4] { "AAA", "aaa", "AAA", "aaa" });
    AddArray(table, new string[4] { "BBB", "bbb", "BBB", "bbb" });
    if (condition)
    {
        AddArray(table, new string[4] { "CCC", "ccc", "CCC", "ccc" });
        AddArray(table, new string[4] { "DDD", "ddd", "DDD", "ddd" });
    }
    else
    {
        AddArray(table, new string[4] { "CCC", "ccc", "XXX", "xxx" });
        AddArray(table, new string[4] { "DDD", "ddd", "YYY", "yyy" });
        AddArray(table, new string[4] { "", "", "CCC", "ccc" });
        AddArray(table, new string[4] { "", "", "DDD", "ddd" });
    }
    ExampleGridView.DataSource = table;
    ExampleGridView.DataBind();
}

private void AddArray(DataTable table, string[] items) {
    var row = table.NewRow();
    row.ItemArray = items;
    table.Rows.Add(row);
}