我有一个带有GridView
<asp:GridView ID="grid_view" runat="server" AllowPaging="True"
AutoGenerateColumns="False" BackColor="White" BorderColor="#999999"
BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black"
GridLines="Vertical">
<FooterStyle BackColor="#CCCCCC" />
<Columns>
<asp:BoundField DataField="adc" HeaderText="Posa No." />
<asp:BoundField DataField="cde" HeaderText="Unit" />
<asp:BoundField DataField="efg" HeaderText="User" />
<asp:BoundField DataField="hj" HeaderText="Posa Date" />
</Columns>
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="#CCCCCC" />
</asp:GridView>
我背后的代码是
protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
gridfil();
}
public void gridfil()
{
con.Open();
cmd = new SqlCommand("select a,b,c from xyz where approved='sss'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
SqlCommandBuilder cb = new SqlCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds);
grid_view.DataSource = ds;
grid_view.DataBind();
//rd.Close();
da.Dispose();
cmd.Dispose();
con.Close();
}
protected void grid_view_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grid_view.PageIndex = e.NewPageIndex;
gridfil();
}
我的问题是网格视图被显示但页面索引无效... 有没有人有任何建议?
答案 0 :(得分:0)
将您的网页加载代码放在!IsPostBack()
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
gridfil();
}
}
原因:每当您点击页码并希望查看其他页面时,页面都会回发,page load event
在grid_view_PageIndexChanging
之前被解雇,它将重新绑定网格视图,您的活动将丢失,并且不会触发PageIndexChanging事件。
其次更改此内容并查看评论
protected void grid_view_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gridfil(); // First bind the gridview
grid_view.PageIndex = e.NewPageIndex; // then change the page Index
}