我的DataGrid显示来自第一页的数据,无论我点击了哪个页面,我都搜索了解决方案,但没有任何效果。我已将!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();
}
上的网格。这是我的代码:
<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>
和我的.aspx
/mnt/c
答案 0 :(得分:1)
@vin,这里好像你启用了属性AllowPaging =&#34; True&#34; &安培; AllowCustomPaging =&#34;真&#34 ;.默认代码为AllowPaging =&#34; True&#34;然后我试着看看下面的例子。可能会帮助你理清你的问题。
你的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>
让我知道解决方案是否适用于您?