How to create a 'Show All' button in GridV

2019-05-27 11:23发布

问题:

When AllowPaging is enabled in a GridView, you can set 4 different types of display modes in the PagerSettings.

<PagerSettings Mode="NextPreviousFirstLast" FirstPageText="First" LastPageText="Last" PageButtonCount="5" Position="Bottom" />

These types are

NextPrevious         : Previous-page and next-page buttons.
NextPreviousFirstLast: Previous-page, next-page, first-page, and last-page buttons.
Numeric              : Numbered link buttons to access pages directly.
NumericFirstLast     : Numbered and first-link and last-link buttons.

Which will look like this

< >
Fist < > Last
1 2 3 4
1 2 3 4 Last

But what I want is all the page numbers and then a "View All" or "Show All" button to display all the Grid rows.

1 2 3 4 - Show All

Is this possible?

回答1:

Yes it is. The trick is that the GridView Pager Control is a nested table inside the GridView. You can use the OnRowCreated event to find that table and add you own "Show All" link.

That link will trigger a command that sets the AllowPaging property to false thus showing all the rows. This will work for every GridView on the page, just add the OnRowCreated event to the GridView.

<asp:GridView ID="GridView1" runat="server" OnRowCreated="pagerViewAll_RowCreated">

And then in code behind

protected void pagerViewAll_RowCreated(object sender, GridViewRowEventArgs e)
{
    //check if the row is the pager row
    if (e.Row.RowType == DataControlRowType.Pager)
    {
        //cast the sender back to a gridview
        GridView gridView = sender as GridView;

        //get the id of the gridview as a string
        string senderID = gridView.ID;

        //create a new linkbutton
        LinkButton linkButton = new LinkButton();
        linkButton.ID = senderID + "_ShowAll";
        linkButton.Text = "Show All";
        linkButton.CommandName = senderID;

        //add the viewAll_Command to the linkbutton
        linkButton.Command += new CommandEventHandler(viewAll_Command);

        //get the first table cell from the pager row (there is only one, spanned across all colums)
        TableCell pagerCell = e.Row.Cells[0];

        //cast the first control found in the pager cell as a table
        Table table = pagerCell.Controls[0] as Table;

        //then the first row of the table containing the pager buttons
        TableRow row = table.Rows[0];

        //add an empty cell for spacing
        TableCell tableCellSpacer = new TableCell();
        tableCellSpacer.Text = "&nbsp;-&nbsp;";
        row.Cells.Add(tableCellSpacer);

        //then add the cell containing the view all button
        TableCell tableCell = new TableCell();
        tableCell.Controls.Add(linkButton);
        row.Cells.Add(tableCell);
    }
}

The View All Method in code behind

protected void viewAll_Command(object sender, CommandEventArgs e)
{
    //check which gridview for setting all the rows visible
    if (e.CommandName == "GridView1")
    {
        //disable pagination and rebuild the grid
        GridView1.AllowPaging = false;
        buildGridView1();
    }
    else if (e.CommandName == "GridView2")
    {
        GridView2.AllowPaging = false;
        buildGridView2();
    }
    else if (e.CommandName == "ListView1")
    {
        DataPager dataPager = ListView1.FindControl("DataPager1") as DataPager;
        dataPager.PageSize = 99999;
        buildListView1();
    }
}

This also works for a DataPager Control used in ListView for example.

private void extendDataPagerInListView()
{
    //find the datapager inside the listview
    DataPager dataPager = ListView1.FindControl("DataPager1") as DataPager;

    //get the id of the listview as a string
    string senderID = ListView1.ID;

    //add some spacing
    Literal literal = new Literal();
    literal.Text = "&nbsp;-&nbsp;";

    //add the literal to the datapager
    dataPager.Controls.Add(literal);

    //create a new linkbutton
    LinkButton linkButton = new LinkButton();
    linkButton.ID = senderID + "_ShowAll";
    linkButton.Text = "Show All";
    linkButton.CommandName = senderID;

    //add the viewAll_Command to the linkbutton
    linkButton.Command += new CommandEventHandler(viewAll_Command);

    //add the linkbutton to the datapager
    dataPager.Controls.Add(linkButton);
}