0

我有一个动态Listview绑定到三个具有一对多关系的不同表,即一个表行可能包含另一个表中的许多行。当我运行我的应用程序时,我得到了这个输出。 原始输出

但是我想以这种格式获取 Listview,尽管这张图片是使用 Photshop 编辑的。

使用 Photoshop 编辑

这里是Listview HTML

<asp:ListView runat="server" ID="LV_ViewQuestion" DataKeyNames="UID, Question_ID">
                <EmptyDataTemplate>
                    <table id="Table1" runat="server" style="">
                        <tr>
                            <td>No Surveys.</td>
                        </tr>
                    </table>
                </EmptyDataTemplate>
                <ItemTemplate>
                    <tr style="">
                        <td>
                            <asp:Label ID="SURVEY_TYPELabel" runat="server" Text='<%# Eval("Survey_Type")%>' />
                        </td>
                        <td>
                            <asp:Label ID="SURVEY_TITLELabel" runat="server" Text='<%# Eval("Survey_Title") %>' />
                        </td>
                        <td>
                            <asp:Label ID="Question_TextLabel" runat="server" Text='<%# Eval("Question_Text")%>' />
                        </td>
                        <td>
                            <asp:Label ID="Label2" runat="server" Text='<%# Eval("Option_Text")%>' />
                        </td>
                        <td>
                            <asp:LinkButton runat="server" ID="lb_DelQuestion" Text="Delete" CommandArgument='<%# Eval("Question_ID")%>' CommandName="XDelQuestion" CssClass="GeneralInput" />&nbsp;
                            <asp:LinkButton runat="server" ID="lb_AddMoreQuest" Text="Add Question" CommandArgument='<%# Eval("UID")%>' CommandName="XAddAnotQuestion" CssClass="GeneralInput" />&nbsp;
                            <asp:LinkButton runat="server" ID="lb_Publish" Text="Publish" CommandArgument='<%# Eval("UID")%>' CommandName="XPublishSurvey" CssClass="GeneralInput" />

                        </td>
                    </tr>
                </ItemTemplate>
                <LayoutTemplate>
                    <table id="Table2" runat="server">
                        <tr id="Tr1" runat="server">
                            <td id="Td1" runat="server">
                                <table id="itemPlaceholderContainer" runat="server" class="nobordered" style="width: 580px;">
                                    <tr id="Tr2" runat="server" style="">
                                        <th id="Th1" runat="server">Type</th>
                                        <th id="Th2" runat="server">Title</th>
                                        <th id="Th6" runat="server">Question</th>
                                        <th id="Th4" runat="server">Options</th>
                                        <th id="Th3" runat="server" style="width: 200px;">Actions</th>
                                    </tr>
                                    <tr id="itemPlaceholder" runat="server">
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr id="Tr3" runat="server">
                            <td id="Td2" runat="server" style="">
                                <asp:DataPager ID="DataPager1" runat="server">
                                    <Fields>
                                        <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowLastPageButton="True" ButtonCssClass="GeneralButton" />
                                    </Fields>
                                </asp:DataPager>
                            </td>
                        </tr>
                    </table>
                </LayoutTemplate>
            </asp:ListView>
4

1 回答 1

1

您可以ItemDataBoundListView. 您的代码应如下所示:

首先在你的页面中添加三个全局属性

    string type = string.Empty;
    string title = string.Empty;
    string question = string.Empty; 

然后将OnItemDataBound事件添加到您的列表视图

protected void LV_ViewQuestion_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        Label SURVEY_TYPELabel = (Label)e.Item.FindControl("SURVEY_TYPELabel");                 
        Label SURVEY_TITLELabel = (Label)e.Item.FindControl("SURVEY_TITLELabel");                 
        Label Question_TextLabel = (Label)e.Item.FindControl("Question_TextLabel");                 

        if (SURVEY_TYPELabel.Text == type && SURVEY_TITLELabel == title && 
            Question_TextLabel == question)
        {
            SURVEY_TYPELabel.Visible = false;
            SURVEY_TITLELabel.Visible = false;
            Question_TextLabel.Visible = false;          
            // Do the same for all the other control in cells you need to hide
        }
        else
        {
            type = SURVEY_TYPELabel.Text;
            title = SURVEY_TITLELabel.Text;
            question = Question_TextLabel.Text;
        }
    }

}
于 2013-01-25T13:03:48.107 回答