0

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

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

基本上,我需要(有条件地)隐藏单元格 XXX xxx YYY 和 yyy。并将它们向上移动。

之前和之后

<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>
4

1 回答 1

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);
}
于 2012-08-03T01:39:17.500 回答