DataGrid 不会显示下一页的数据
DataGrid won't show data of the next page
无论我单击哪个页面,我的 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>
<asp:TemplateColumn HeaderText="ID" >
<HeaderStyle Font-Underline="false" Height="15px" Width="5%" HorizontalAlign="Center" BackColor="#ccffcc"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:Label id="lblID" runat="server" text='<%#DataBinder.Eval(Container.DataItem, "ID_")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<PagerStyle Font-Bold="True" ForeColor="black" HorizontalAlign="Center" Wrap="True" Mode="NumericPages" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
</asp:DataGrid>
@vin,这里您似乎同时启用了 属性 AllowPaging="True" 和 AllowCustomPaging="True"。它的默认代码是 AllowPaging="True" 然后我尝试使用它,请参见下面的示例。可能会帮助您解决问题。
您的 aspx.cs 代码应该与
类似
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
protected void grid1_PageIndexChanging(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
grid1.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
private int GetNumItems()
{
int totalRow = 15;
return totalRow;
}
private void BindGrid()
{
grid1.DataSource = GetTable();
grid1.DataBind();
}
public DataTable GetTable()
{
DataTable table = new DataTable();
table.Columns.Add("ID", typeof(int));
table.Columns.Add("Name", typeof(string));
table.Rows.Add(1, "David");
table.Rows.Add(2, "Sam");
table.Rows.Add(3, "Christoff");
table.Rows.Add(4, "Janet");
table.Rows.Add(5, "Melanie");
table.Rows.Add(6, "David1");
table.Rows.Add(7, "Sam1");
table.Rows.Add(8, "Christoff1");
table.Rows.Add(9, "Jane1t");
table.Rows.Add(10, "Melanie1");
return table;
}
您的 .aspx 代码应该类似于
<asp:DataGrid ID="grid1" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" Width="100%"
AllowPaging="True" PageSize="5"
OnPageIndexChanged="grid1_PageIndexChanging"
>
<AlternatingItemStyle CssClass="tdgenap" />
<ItemStyle CssClass="tdganjil" HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" Height="30px" CssClass="tdjudul"></HeaderStyle>
<Columns>
<asp:TemplateColumn HeaderText="ID">
<HeaderStyle Font-Underline="false" Height="15px" Width="5%" HorizontalAlign="Center" BackColor="#ccffcc"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "ID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle Font-Bold="True" ForeColor="black" HorizontalAlign="Center" Wrap="True" Mode="NumericPages" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
</asp:DataGrid>
请告诉我该解决方案是否适合您?
无论我单击哪个页面,我的 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>
<asp:TemplateColumn HeaderText="ID" >
<HeaderStyle Font-Underline="false" Height="15px" Width="5%" HorizontalAlign="Center" BackColor="#ccffcc"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:Label id="lblID" runat="server" text='<%#DataBinder.Eval(Container.DataItem, "ID_")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<PagerStyle Font-Bold="True" ForeColor="black" HorizontalAlign="Center" Wrap="True" Mode="NumericPages" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
</asp:DataGrid>
@vin,这里您似乎同时启用了 属性 AllowPaging="True" 和 AllowCustomPaging="True"。它的默认代码是 AllowPaging="True" 然后我尝试使用它,请参见下面的示例。可能会帮助您解决问题。
您的 aspx.cs 代码应该与
类似 protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
protected void grid1_PageIndexChanging(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
grid1.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
private int GetNumItems()
{
int totalRow = 15;
return totalRow;
}
private void BindGrid()
{
grid1.DataSource = GetTable();
grid1.DataBind();
}
public DataTable GetTable()
{
DataTable table = new DataTable();
table.Columns.Add("ID", typeof(int));
table.Columns.Add("Name", typeof(string));
table.Rows.Add(1, "David");
table.Rows.Add(2, "Sam");
table.Rows.Add(3, "Christoff");
table.Rows.Add(4, "Janet");
table.Rows.Add(5, "Melanie");
table.Rows.Add(6, "David1");
table.Rows.Add(7, "Sam1");
table.Rows.Add(8, "Christoff1");
table.Rows.Add(9, "Jane1t");
table.Rows.Add(10, "Melanie1");
return table;
}
您的 .aspx 代码应该类似于
<asp:DataGrid ID="grid1" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" Width="100%"
AllowPaging="True" PageSize="5"
OnPageIndexChanged="grid1_PageIndexChanging"
>
<AlternatingItemStyle CssClass="tdgenap" />
<ItemStyle CssClass="tdganjil" HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" Height="30px" CssClass="tdjudul"></HeaderStyle>
<Columns>
<asp:TemplateColumn HeaderText="ID">
<HeaderStyle Font-Underline="false" Height="15px" Width="5%" HorizontalAlign="Center" BackColor="#ccffcc"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "ID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle Font-Bold="True" ForeColor="black" HorizontalAlign="Center" Wrap="True" Mode="NumericPages" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
</asp:DataGrid>
请告诉我该解决方案是否适合您?