无论我单击哪个页面,我的 DataGrid 都会显示来自第一页的数据,我已经搜索了解决方案,但没有任何效果。我已经把BindGrid对!IsPostBack和重新绑定在网格grid1_PageIndexChanging。这是我的代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
private int GetNumItems()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = gc.GetWebConfigConnectionStringAIS();
con.Open();
string query = "SELECT COUNT(*) FROM dbo.TestingLatihan";
SqlCommand cmd = new SqlCommand(query, con);
Int32 totalRow = (Int32)cmd.ExecuteScalar();
con.Close();
return totalRow;
}
protected void grid1_PageIndexChanging(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
grid1.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
private void BindGrid()
{
grid1.VirtualItemCount = GetNumItems();
grid1.PageIndexChanged += new DataGridPageChangedEventHandler(grid1_PageIndexChanging);
SqlConnection con = new SqlConnection();
con.ConnectionString = gc.GetWebConfigConnectionStringAIS();
con.Open();
string query = "SELECT * FROM dbo.TestingLatihan";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataAdapter myAdapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
myAdapter.Fill(dt);
grid1.DataSource = dt;
grid1.DataBind();
con.Close();
}
和我的 .aspx
<asp:DataGrid ID="grid1" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" Width="100%"
OnItemCommand="dtgItemCommand" AllowPaging="True" PageSize="5"
AllowCustomPaging="True" OnPageIndexChanged="grid1_PageIndexChanging"
EnableViewState="true">
<AlternatingItemStyle CssClass="tdgenap" />
<ItemStyle CssClass="tdganjil" HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" Height="30px" CssClass="tdjudul"></HeaderStyle>
<Columns>
相关分类