简体   繁体   English

ASP.Net GridView不显示页码

[英]ASP.Net GridView not displaying page numbers

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!" 一切都基本上说,“哦,只需打开AllowPaging,你就完成了!” 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. 当我这样做时,是的,我可以在设计视图中看到GridView下的分页控件,但是当我编译时,我无法在运行的站点中看到页码。

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: 因此我的GridView很简单:

<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. 我从后面做数据工作的意思是,所有列和所有内容都是从代码构建到DataTable中,然后我将GridView的DataSource设置为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... 这只是我个人的偏好,说实话我实际上从未学过如何使用DataSource控件以及所有示例都在使用的地方,你在.aspx文件中使用列,数据源构建GridView所以我我的问题在于大方向......

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? 将“AllowPaging”设置为true真的是我需要做的所有事情吗?

For Paging to work, your datasource must support it. 要使Paging工作,您的数据源必须支持它。 If it does not, like a DataTable, then you have to do this yourself. 如果没有,就像DataTable一样,那么你必须自己做。

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

you have to use the page_index changing event in the gridview to implement paging in the gridview refer this link: 你必须使用gridview中的page_index更改事件来实现gridview中的分页,请参阅以下链接:

http://forums.asp.net/t/1245611.aspx http://forums.asp.net/t/1245611.aspx

hope it helps 希望能帮助到你

You can disable particular column and add Paging 您可以禁用特定列并添加分页

protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.DataRow)
//----------------------------------Grid view column invisible------------------------------------------------------------
                if (Request.QueryString.Get("show") == "all")
                    GridView1.Columns[0].Visible = true;
                else
                    GridView1.Columns[0].Visible = false;

                //-------------------------------------------------------------------------------------------------------------------------

}

protected void Gridview1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

            GridView1.PageIndex = e.NewPageIndex;
            gvbind();// Grid View Binded

        }

// Source Code
allowpaging="true" OnPageIndexChanging="Gridview1_PageIndexChanging" pagesize="2"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM