gpt4 book ai didi

c# - 如何在 GridView Pager 或 DataPager 中创建一个 'Show All' 按钮

转载 作者:行者123 更新时间:2023-11-30 23:18:48 28 4
gpt4 key购买 nike

当在 GridView 中启用 AllowPaging 时,您可以设置 4 种不同类型的 display modesPagerSettings 中。

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

这些类型是

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.

看起来像这样

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

但我想要的是所有页码,然后是“查看全部”或“显示全部”按钮以显示所有网格行。

1 2 3 4 - Show All

这可能吗?

最佳答案

是的。诀窍在于 GridView Pager Control 是 GridView 内的嵌套表格。您可以使用 OnRowCreated 事件找到该表并添加您自己的“显示全部”链接。

该链接将触发将 AllowPaging 属性设置为 false 的命令,从而显示所有行。这将适用于页面上的每个 GridView,只需将 OnRowCreated 事件添加到 GridView。

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

然后在代码后面

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);
}
}

代码隐藏的View All方法

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();
}
}

例如,这也适用于 ListView 中使用的 DataPager 控件。

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);
}

关于c# - 如何在 GridView Pager 或 DataPager 中创建一个 'Show All' 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40636127/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com