gridview自定义分页无法触发PageIndexChanging事件

sindel 2008-08-29 01:33:32
<pagertemplate>
<table width="100%">
<tr>
<td style="text-align:right">
第<asp:Label id="lblPageIndex" runat="server" text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>' />页
共/<asp:Label id="lblPageCount" runat="server" text='<%# ((GridView)Container.Parent.Parent).PageCount %>' />页
<asp:linkbutton id="btnFirst" runat="server" causesvalidation="False" commandargument="First" commandname="Page" text="首页" />
<asp:linkbutton id="btnPrev" runat="server" causesvalidation="False" commandargument="Prev" commandname="Page" text="上一页" />
<asp:linkbutton id="btnNext" runat="server" causesvalidation="False" commandargument="Next" commandname="Page" text="下一页" />
<asp:linkbutton id="btnLast" runat="server" causesvalidation="False" commandargument="Last" commandname="Page" text="尾页" />
<asp:textbox id="txtNewPageIndex" runat="server" width="20px" text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>' />
<asp:linkbutton id="btnGo" runat="server" causesvalidation="False" commandargument="-1" commandname="Page" text="GO" /><!-- here set the CommandArgument of the Go Button to '-1' as the flag -->
</td>
</tr>
</table>
</pagertemplate>

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView theGrid = sender as GridView; // refer to the GridView
int newPageIndex = 0;

if (-2 == e.NewPageIndex) { // when click the "GO" Button
TextBox txtNewPageIndex = null;
//GridViewRow pagerRow = theGrid.Controls[0].Controls[theGrid.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate
GridViewRow pagerRow = theGrid.BottomPagerRow; //GridView较DataGrid提供了更多的API,获取分页块可以使用BottomPagerRow 或者TopPagerRow,当然还增加了HeaderRow和FooterRow
//updated at 2006年6月21日3:15:33

if (null != pagerRow) {
txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox; // refer to the TextBox with the NewPageIndex value
}

if (null != txtNewPageIndex) {
newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex
}
}
else { // when click the first, last, previous and next Button
newPageIndex = e.NewPageIndex;
}

// check to prevent form the NewPageIndex out of the range
newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;

// specify the NewPageIndex
theGrid.PageIndex = newPageIndex;
//theGrid.PageIndex = e.NewPageIndex;

// rebind the control
// in this case of retrieving the data using the xxxDataSoucr control,
// just do nothing, because the asp.net engine binds the data automatically

theGrid.DataSource = DB.ODateSet;
theGrid.DataBind();
DB.free();
}

这是网上一个分页例子,单步测试了,点击下一步都无法触发GridView1_PageIndexChanging事件,gridview是空白一片,各位帮帮忙吧,谢谢了!!


...全文
1579 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
CalvinR 2011-11-23
  • 打赏
  • 举报
回复
onlyyi 2009-10-02
  • 打赏
  • 举报
回复
给GridView控件添加一个事件PageIndexChanging,并设置GridView的属性:AllowPaging="True"

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.datasource="数据源";
GridView1.DataBind();
}
这个方法行.
lizhw86 2009-08-06
  • 打赏
  • 举报
回复
你将分页代码写到Row_Cammnd函数里面吧。
sindel 2008-09-08
  • 打赏
  • 举报
回复
EnableViewState="False" 改为true,闪分
jadedm 2008-09-04
  • 打赏
  • 举报
回复
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
DataGridDataBind();
}
}

//绑定数据
private void DataGridDataBind()
{
DataSet ds = GetCustomersData();
recordCount = ds.Tables[0].Rows.Count;
//获取当前的页数
pageCount = (int)Math.Ceiling( recordCount * 1.0 / PageSize);
//避免纪录从有到无时,并且已经进行过反页的情况下CurrentPageIndex > PageCount出错
if(recordCount ==0)
{
this.DataGrid1.CurrentPageIndex = 0;
}
else if(this.DataGrid1.CurrentPageIndex >= pageCount)
{
this.DataGrid1.CurrentPageIndex = pageCount - 1;
}
this.DataGrid1.DataSource = ds;
this.DataGrid1.DataBind();
NavigationStateChange();
}
sjzl80 2008-09-02
  • 打赏
  • 举报
回复
给GridView控件添加一个事件PageIndexChanging,并设置GridView的属性:AllowPaging="True"

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.datasource="数据源";
GridView1.DataBind();
}


sindel 2008-08-29
  • 打赏
  • 举报
回复
自己顶
sindel 2008-08-29
  • 打赏
  • 举报
回复
PagerSettings 我没用PageSetting,不是拖上去的,我是直接把分页模板的代码复制上去,AllowPaging = true;是因为网上的那个例子就是把他设为true,测试发现分页模板的内容把自动生成的分页内容给隐藏了,把模板内容删除后,自动分页产生的分页内容会还原,我想应该不是这个问题吧
sindel 2008-08-29
  • 打赏
  • 举报
回复
<asp:GridView ID="GridView1" runat="server" CellPadding="3"
HorizontalAlign="Center" Width="980px" AutoGenerateColumns="False"
OnPageIndexChanging="GridView1_PageIndexChanging" Font-Size="Smaller"
PageSize="5" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None"
BorderWidth="1px" CssClass="tb_Inner" OnRowDataBound="GridView1_RowDataBound"
EmptyDataText="<center>没有搜索到任何记录,请重置搜索条件。</center>" ForeColor="Red"
EnableViewState="False" AllowPaging="True">

这个我写了..,gridview一样是空白一片
bijiniye1988 2008-08-29
  • 打赏
  • 举报
回复
你意思是,这个下一页,上一页什么的
都是自己拖上去的button?

如果是这样的话应该不行吧

因为如果使用了GridView自己的分页的话

它便自动会有下一页,上一页的按钮

在属性菜单里,PagerSettings设置
  • 打赏
  • 举报
回复
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
autogeneratecolumns="true"
emptydatatext="No data available."
allowpaging="true"
autogenerateeditbutton="true"
datakeynames="CustomerID"
onpageindexchanging="CustomersGridView_PageIndexChanging"
onrowcancelingedit="CustomersGridView_RowCancelingEdit"
runat="server">
sindel 2008-08-29
  • 打赏
  • 举报
回复
说得不清楚,可能误解,我的意思是说项这样配置
<asp:linkbutton id="btnNext" runat="server" causesvalidation="False" commandargument="Next" commandname="Page" text="下一页" />
点击下一页,那个GridView1_PageIndexChanging事件无法激发
bijiniye1988 2008-08-29
  • 打赏
  • 举报
回复
你试着在事件中手都判断一下这个CommandArgument

然后进行操作试试

如果你确认GridView可以自动识别Prev这个CommandArgument的话

那就算了
bijiniye1988 2008-08-29
  • 打赏
  • 举报
回复
自动识别吗?

我自己很少用GridView自带的那个分页

都是使用自己的分页,所以不是很熟悉这种方式的分页

你试过PageIndexChanged这个事件吗?

不好意思,这方面的知识我需要再学习学习

可能帮不到什么忙
sindel 2008-08-29
  • 打赏
  • 举报
回复
AllowPaging = true; 设置好了,没用

commandargument="Prev" 这样设置好这个参数GridView1_PageIndexChanging事件是会自动识别的吧??
还有什么可能导致 GridView1_PageIndexChanging事件无法触发呢??
bijiniye1988 2008-08-29
  • 打赏
  • 举报
回复
那你是否手动设置了这个GridView的分页属性

比如:
AllowPaging = true;
等等

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧