dEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGridDataBind();
}
//获取当前项
protected int GetRecordIndex(int itemIndex)
{
return (DataGrid1.CurrentPageIndex * DataGrid1.PageSize + itemIndex + 1);
}
private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
DataGrid dg = (DataGrid)sender;
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Cells[0].Text = (dg.CurrentPageIndex * dg.PageSize + e.Item.ItemIndex + 1).ToString();
e.Item.Cells[1].Text = (dg.CurrentPageIndex * dg.PageSize + e.Item.ItemIndex + 1).ToString();
}
}
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
DataGrid dg = (DataGrid)sender;
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
((Label)e.Item.FindControl("itemIndex")).Text = (dg.CurrentPageIndex * dg.PageSize + e.Item.ItemIndex + 1).ToString();
}
}
}
}
数据层代码如下:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace AspnetPaging
{
public class DataAccess
{
private static string connString = ConfigurationSettings.AppSettings["ConnString"];
private DataAccess()
{
}
public static DataSet GetCustomersData()
{
SqlConnection conn = new SqlConnection(connString);
SqlCommand comm = new SqlCommand("GetCustomers",conn);
comm.CommandType = CommandType.StoredProcedure;
SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
return ds;
}
}
}
总结,上面的四种方法前两种其实处理起来是一样的,只是处理的时间不同而已;对于第三种我认为最简单,直接在前台页面绑定,不需要额外的辅助;对于第四种的方法绑定到前台我认为最为灵活,需要注意的是GetRecordIndex方法需要protected或public,使它的继承类能访问的到。
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGridDataBind();
}
//获取当前项
protected int GetRecordIndex(int itemIndex)
{
return (DataGrid1.CurrentPageIndex * DataGrid1.PageSize + itemIndex + 1);
}
private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
DataGrid dg = (DataGrid)sender;
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Cells[0].Text = (dg.CurrentPageIndex * dg.PageSize + e.Item.ItemIndex + 1).ToString();
e.Item.Cells[1].Text = (dg.CurrentPageIndex * dg.PageSize + e.Item.ItemIndex + 1).ToString();
}
}
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
DataGrid dg = (DataGrid)sender;
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
((Label)e.Item.FindControl("itemIndex")).Text = (dg.CurrentPageIndex * dg.PageSize + e.Item.ItemIndex + 1).ToString();
}
}
}
}
数据层代码如下:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace AspnetPaging
{
public class DataAccess
{
private static string connString = ConfigurationSettings.AppSettings["ConnString"];
private DataAccess()
{
}
public static DataSet GetCustomersData()
{
SqlConnection conn = new SqlConnection(connString);
SqlCommand comm = new SqlCommand("GetCustomers",conn);
comm.CommandType = CommandType.StoredProcedure;
SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
return ds;
}
}
}
总结,上面的四种方法前两种其实处理起来是一样的,只是处理的时间不同而已;对于第三种我认为最简单,直接在前台页面绑定,不需要额外的辅助;对于第四种的方法绑定到前台我认为最为灵活,需要注意的是GetRecordIndex方法需要protected或public,使它的继承类能访问的到。
| 对此文章发表了评论 |

