在C#中创建选项卡式Gridview

时间:2009-10-21 20:25:21

标签: c# gridview tabs

我正在创建一个Web界面,该界面当前从XML数据文件中读取数据,将其解析为一个Object数组,然后将其绑定到Gridview的数据源。然后我使用和从每个列的对象中检索我想要的数据。

但是,我希望在这个gridview中有多个标签,可能由不同的链接按钮控制。每个选项卡都会显示一组不同的列。

实施此操作的最佳方法是什么?我是否需要在我的页面中有三个单独的GridView,并且只显示用户选择的那个(基于点击链接按钮),同时隐藏所有其他的?这似乎可能不必要地缓慢。是否可以通过一个GridView进行?

现在整个GridView都包含在AJAX更新面板中,代码如下:

<asp:Panel id="searchResultsGrid" runat="server" CssClass="searchResultsGrid">
    <asp:GridView id="gridViewSearchResults" runat="server" AutoGenerateColumns="false" 
                    AllowPaging="True" AllowSorting="True" 
                    PageSize="25" Width="920" PagerSettings-Visible="false">
    <Columns>
        <asp:templatefield headertext="Test Column 1 Tab 1" HeaderStyle-HorizontalAlign="Left">
            <itemtemplate>
                <%# GetColumnInfo() %>
            </itemtemplate>
        </asp:templatefield>
        <asp:templatefield headertext="Test Column 2 Tab 1" HeaderStyle-HorizontalAlign="Left">
            <itemtemplate>
                <%# GetColumnInfo() %>
            </itemtemplate>
        </asp:templatefield>
        <asp:templatefield headertext="Test Column 3 Tab 1" HeaderStyle-HorizontalAlign="Left">
            <itemtemplate>
                <%# GetColumnInfo() %>
            </itemtemplate>
        </asp:templatefield>
        <asp:templatefield headertext="Test Column 4 Tab 1" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right">
            <itemtemplate>
                <%# GetColumnInfo() %>
            </itemtemplate>
        </asp:templatefield>
        <asp:templatefield headertext="Test Column 5 Tab 1" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right">
            <itemtemplate>
                <%# GetColumnInfo() %>
            </itemtemplate>
        </asp:templatefield>
        <asp:templatefield headertext="Test Column 6 Tab 1" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right">
            <itemtemplate>
                <%# GetColumnInfo() %>
            </itemtemplate>
        </asp:templatefield>
        <asp:templatefield headertext="Test Column 7 Tab 1" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right">
            <itemtemplate>
                <%# GetColumnInfo() %>
            </itemtemplate>
        </asp:templatefield>                                                                                                    
    </Columns>
    <RowStyle CssClass="searchResultEntry borderTopGrey" />         
    <EmptyDataTemplate>
        <p class="searchResultsEmpty">
            <asp:Label ID="lblSearchResultsEmpty" runat="server" Text="No records matched the selected criteria.  Please revise your criteria and try again." CssClass="searchResultsEmpty"></asp:Label>
        </p>
    </EmptyDataTemplate>                
    </asp:GridView>
</asp:Panel>

这是我目前为一个gridview提供的代码,其中包含一个选项卡的内容。基于jdk的响应,我如何为第二个和第三个选项卡添加其他TemplateFields,然后在单击选项卡链接按钮时切换显示不同的设置?

谢谢!

3 个答案:

答案 0 :(得分:1)

当存在多个GridView时,页面ViewState字符串会变得非常(不必要地)大(查看其生成的HTML源代码并搜索“__VIEWSTATE”以查看它)。你可以像你说的那样使用一个GridView控件,并根据最近点击的哪个LinkBut​​ton(a.k.a。“tab”)将适当的数据交换到它中。

如果这也是一个分页数据场景,你可以在ViewState中存储一个三个整数的简单数组,表示三组数据中每一组的当前页码,这样你就可以在交换它们时显示最新的数据页面进出一个DataGrid控件。

但是,如果不考虑带宽(即,如果页面没有收到大量的点击或在Intranet上运行),那么不要过于担心优化它。

答案 1 :(得分:0)

我之前做过类似的事情。我使用了GridView的模板列。并在GridView中放置Ajax control toolkit tab control

答案 2 :(得分:0)

我可能会创建一个自定义复合控件(作为选项卡式容器)并添加网格视图。我不会把它捆绑成一个。