有志者求gridview问题解答
<%@ Page Language= "C# " AutoEventWireup= "true " CodeFile= "pagechanging.aspx.cs " Inherits= "pagechanging " %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> Untitled Page </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
<table>
<tr>
<td>
<asp:Button ID= "bt1 " runat=server Text= "查询 " OnClick= "bt1_Click " />
</td>
</tr>
<tr>
<td>
<asp:GridView ID= "gridview1 " AllowPaging=true runat=server AutoGenerateColumns=false OnPageIndexChanging= "gridview1_PageIndexChanging " PageSize= "5 ">
<Columns>
<asp:BoundField HeaderText= "名称 " DataField= "job_desc " />
<asp:BoundField HeaderText= "最小数 " DataField= "min_lvl " />
<asp:BoundField HeaderText= "最大数 " DataField= "max_lvl " />
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class pagechanging : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gridview1.DataBind();
}
}
protected void bt1_Click(object sender, EventArgs e)
{
SqlConnection con = DB.createConnection();
con.Open();
DataSet ds = new DataSet();
string s = "select * from jobs ";
SqlDataAdapter My = new SqlDataAdapter(s,con);
My.Fill(ds, "s ");
gridview1.DataSource = ds.Tables[ "s "].DefaultView;
gridview1.DataBind();
}
protected void gridview1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gridview1.PageIndex = e.NewPageIndex;
gridview1.DataBind();
}
}
以上是我自己写的代码,想实现分页,但是第一页正常,点翻页按钮后,都是空白,请问怎么改?大侠们给个答案把,谢谢了,入门难呀!
[解决办法]
是没有显示吗?
[解决办法]
翻页要改变当前页的值呀,你这个里面没有
[解决办法]
现在没VS工具,回答不了你
[解决办法]
还是自定义分页吧,这样也没有什么用,都是先把数据全部读出的
[解决办法]
protected void gridview1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gridview1.PageIndex = e.NewPageIndex;
gridview1.DataBind(); //应该是再次绑定数据,而不是直接调用databind()方法。
}
改成:
public partial class pagechanging : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridViewGetData();
}
}
protected void bt1_Click(object sender, EventArgs e)
{
GridViewGetData()
}
protected void gridview1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gridview1.PageIndex = e.NewPageIndex;
GridViewGetData();
}
private void GridViewGetData()
{
SqlConnection con = DB.createConnection();
con.Open();
DataSet ds = new DataSet();
string s = "select * from jobs ";
SqlDataAdapter My = new SqlDataAdapter(s,con);
My.Fill(ds, "s ");
gridview1.DataSource = ds.Tables[ "s "].DefaultView;
gridview1.DataBind();
}