ASP.Net GridView不显示页码 [英] ASP.Net GridView not displaying page numbers

查看:80
本文介绍了ASP.Net GridView不显示页码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了几篇文章和教程,但我无法弄清楚。一切都基本上说,哦,只要打开AllowPaging,你就完成了!当我这样做的时候,是的,我可以在设计视图中看到GridView下的分页控件,但是当我编译时,我看不到正在运行的网站中的页码。



我注意到有一点与所有例子不同,我从代码隐藏中完成数据工作。因此,我的GridView只是:

pre $ lt; code>< asp:GridView ID =gvlatestrunat =serverwidth =99% AllowSorting =True
onrowdatabound =gvlatest_RowDataBoundonsorting =gvlatest_Sorting
AllowPaging =TruePageSize =2/>

我的意思是通过后面的数据工作,所有的列和所有东西都被构造从代码到DataTable中,然后将GridView的DataSource设置为DataTable。例如,我有一个非常简短的版本:

  DataTable temptable = new DataTable(); 
DataColumn titlecol = new DataColumn();
titlecol.ColumnName =Title;
temptable.Columns.Add(titlecol);
gvlatest.DataSource = temptable;
gvlatest.DataBind();

这只是我个人的偏好,说实话我其实从未学过如何使用DataSource控件,并且使用所有示例,在.aspx文件中使用列,数据源等构建GridView。所以我在猜测我的问题在于:总体方向......



问题是,我做错了什么?为什么页码不显示?是否将AllowPaging设置为true真的是我需要做的所有事情?

为解决方案

数据源必须支持它。如果它不像DataTable一样,那么你必须自己做这件事。



这段代码应该有帮助。



< pre $ OnPageIndexChanging =myGridview_PageIndexChanging

protected void myGridview_PageIndexChanging(object sender,GridViewPageEventArgs e)
{
GridView gv =(GridView)sender ;
DataView dv = gv.DataSource作为DataView;
DataTable dataTable = dv.Table;

gv.DataSource = myDataTable;
gv.PageIndex = e.NewPageIndex;
gv.DataBind();
}


I've gone through several articles and tutorials, but I just can't figure this out. Everything basically says, "oh just turn on AllowPaging, and you're done!" When I do that, yes, I can see the paging controls under the GridView in the Design View, but when I compile, I can't see the page numbers in the running site.

One thing I noticed different from all of the examples, is that I do the data work from the code-behind. Therefore my GridView is simply:

<asp:GridView ID="gvlatest" runat="server" Width="99%" AllowSorting="True" 
              onrowdatabound="gvlatest_RowDataBound" onsorting="gvlatest_Sorting" 
              AllowPaging="True" PageSize="2" />

What I mean by doing the data work from behind, is that all the columns and everything, are constructed from code into a DataTable, and then I set the GridView's DataSource to the DataTable. For example, a grossly abbreviated version of what I have:

DataTable temptable = new DataTable();
DataColumn titlecol = new DataColumn();
titlecol.ColumnName = "Title";
temptable.Columns.Add(titlecol);
gvlatest.DataSource = temptable;
gvlatest.DataBind();

This is just a personal preference I guess, and to be honest I've actually never learned how to use the DataSource controls and such that all the examples are using, where you build the GridView in the .aspx file with the columns, data source, etc. So I'm guessing that my problem lies in that general direction...

The question is, what am I doing wrong? Why are the page numbers not showing up? Is setting "AllowPaging" to true really all that I need to do?

解决方案

For Paging to work, your datasource must support it. If it does not, like a DataTable, then you have to do this yourself.

This code should help.

OnPageIndexChanging="myGridview_PageIndexChanging"

protected void myGridview_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView gv = (GridView)sender;
    DataView dv = gv.DataSource as DataView;
    DataTable dataTable = dv.Table;

    gv.DataSource = myDataTable;
    gv.PageIndex = e.NewPageIndex;
    gv.DataBind();
}

这篇关于ASP.Net GridView不显示页码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆