English 中文(简体)
Asp.net C# 无法工作
原标题:Paging Next in DataGrid in Asp.net C# Not Working

I am trying to do paging of data. Basically I am taking a data and want to show it on multi pages. But it is not working. I am using Asp.net and C# for coding. I am using mysql as database.

Code is as follows : ASP code

<asp:DataGrid runat="server" ID="RestData"
     AllowPaging="True" PageSize="2" 
     OnPageIndexChanged="RestData_PageIndexChanged" AllowCustomPaging="True" 
     PagerStyle-Wrap="False">
     <PagerStyle />
</asp:DataGrid>

C# 代码 :

protected void Page_Load(object sender, EventArgs e)
 {
    BindData();
 }
public void BindData()
    {
     RestData.DataSource = call.GetReader(Convert.ToInt32(AreaData.SelectedValue));  
       //GetReader is function which returns the data reader of mysql 
     RestData.DataBind();
    }
protected void RestData_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
       RestData.CurrentPageIndex = e.NewPageIndex;
        BindData();
   }

Output: It is displaying two rows(as i have given pagesize 2). But I am not able to see next page.

查询应返回超过两行( 当我使用中继器时会发生, 但我无法在中继器中进行拨号 ) 。

问题回答

尝试添加

If(! IsPostBack)
{
   BindData();
}

这里的例子与您所看到的例子相似 DataGrid自定义呼叫

以下部分更适合 Gridview ,而不是 DataGrid

您应该订阅 PageIndexchanging , 而不是 PageIndexchanged 事件。

pageIndex changing 发生于单击一个按位按钮并在网格处理传呼操作之前

PageIndex changed 正在发送操作 。

我还会在页数中填上一张 宾德数据的数据的支票

当您试图更改页面时, DataGrid 必须知道该怎么做 。

在你的网格中(例如来自我在VB的工程,对不起):

 <asp:DataGrid ID="MainGrid"
    OnEditCommand="MainGrid_Edit"
    OnCancelCommand="MainGrid_Cancel"
    OnUpdateCommand="MainGrid_Update"
    runat="server"
    AutoGenerateColumns="False"
    AllowSorting="True"
    CssClass="DistPortalDataGrid"
    AllowPaging="True"
    CellPadding="3"
    PageSize="25" 
    onpageindexchanging="MainGrid_PageIndexChanged"
    ....

注意最后一行...

现在转到代码视图并创建以下子项:

Protected Sub MainGrid_PageIndexChanged(source As Object, e As DataGridPageChangedEventArgs) Handles MainGrid.PageIndexChanged
    MainGrid.CurrentPageIndex = e.NewPageIndex
    BindMainGrid()  rebinds the grid
End Sub

>>Rajuu Parmar 所示,没有数据Grid的 < code>.PageIndex 属性。这是正确的。DataGrid的等同属性是 .CrentralPageIndex 。微软为什么把它们变换了,我不知道。虽然你已经解决问题(几年前),但我希望你或其他人会发现这一点有用。

您还需要告诉网格总共有多少项, 例如 :

public void BindData() {
    RestData.VirtualItemCount = CountTotalItemsInDb();
    // ... the rest ....
}

The reason why paging on your datagrid not working was because you had created protected method and it wasn t able to access it. If the code is change to below, it would have worked. Hope it helps others in need.

public void RestData_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
       RestData.CurrentPageIndex = e.NewPageIndex;
        BindData();
   }

Thanks for your help. I got the solution. Just posting the solution if some one need in future. Finally I got the solution by GridView. It does do paging.

ASP 守则

<asp:GridView ID="RestGridData" runat="server"
     AllowPaging="True"  AutoGenerateColumns="False"
     PageSize="2" onpageindexchanging="RestGridData_PageIndexChanging">
  </asp:GridView> 

CS 代码码( CS 代码)

protected void Page_Load(object sender, EventArgs e)
    {
       GridBindData(); // on page load 
    }
public void GridBindData()
    {
        MySqlConnection Conn; // I am using MySql 
        myConn = new MySqlConnection("server=localhost;user=root;database=DBName;");
        conn.Open();
        MySqlCommand cmd = new MySqlCommand("Select Name, address, mobileNo, emailID from  user", conn);
         MySqlDataReader reader = cmd.ExecuteReader();
        // As DataReader can move only in forward it is not useful to use it GridView.
       // So convert it to DataTable(or?). It can move in both direction

        DataTable dTable = new DataTable();
        dTable.Load(reader); // ASP function to convert reader to DataTable

        RestGridData.DataSource = dTable; 
        RestGridData.DataBind();

    }
protected void RestGridData_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        RestGridData.PageIndex = e.NewPageIndex;
        GridBindData();
    }

试试这个

protected void G_BgtPersonnel_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    //Next page in the GridView
    G_BgtPersonnel.PageIndex = e.NewPageIndex;
    BindgrdPersonnal();
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!