Creating a tabbed Gridview in C#
I am creating a web interface which currently reads data in from an XML data file, parses it into an array of Objects, after which I bind it to the data source of a Gridview. I then use and to retrieve the data I want from the objects for each column.
However, I am at the point that I would like to have multiple tabs in this gridview, possibly controlled by different link buttons. Each tab would show a different set of columns.
What would be the best way to implement this? Do I need to hav开发者_如何学编程e three separate GridViews in my page, and just show the one for which the user selected (based on the click to the link button), while hiding all the others? This seemed like it might be unnecessarily slow. Is it possible to do via one GridView?
Right now the entire GridView is contained in an AJAX update panel, with the code below:
<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>
This is the code I currently have for one gridview, with the content of one tab. Based on jdk's response, how would I go about adding other TemplateFields for the second and third tabs, and then switching between displaying the different sets when a tab link button is clicked?
Thanks!
The page ViewState string can become very (unnecessarily) large when multiple GridViews are present (view its resulting HTML source code and search for "__VIEWSTATE" to see it). You can use one GridView control, like you said, and swap appropriate data into it depending on which LinkButton (a.k.a. "tab") was recently clicked.
If this is also a paginated data scenario, you can store a simple array of three integers in ViewState representing the current page number of each of the three sets of data, so you can display the most recent page of data when swapping them in and out of the one DataGrid control.
However if bandwidth is not a concern (i.e. if the page doesn't receive a lot of hits or runs on an Intranet) then don't worry as much about optimizing it.
I've done similar things before. I used template columns for the GridView. And put a Ajax control toolkit tab control in the GridView.
I would probably create a Custom Composite Control (as the tabbed-container) and add the grid-view to that. I would not bundle it into one.
精彩评论