在areaID不为空的时候重新绑定SqlDataSource,但我将代码放在if (!IsPostBack)中,翻页时总时会显示之前绑定SqlDataSource的数据,而如果把代码放在if (!IsPostBack)外,就没法翻页。代码如下:
protected void Page_Load(object sender, EventArgs e)
{
//添加地区
if (!IsPostBack)
{
DataBase DB = new DataBase();
DataSet myDs2 = new DataSet();
string strsql = "";
string areaID = "";
string sql = "SELECT a.departmentID AS departmentID,a.name AS name,b.name as zkzbName, d.name AS hospitalName, b.sort as sortName,c.name AS provinceName FROM osw_department a ,osw_dictionary_zkzb b ,studyask_area c ,osw_hospital d where a.sort = b.sort and d.hospitalID=a.hospital and a.province=c.areaID and b.id=a.zkzb";
if ((Request.Params["areaID"] != null))
{
areaID = Request.Params["areaID"];
if (!strsql.Equals(""))
{
strsql = strsql + " and ";
}
strsql = strsql + "a.province='" + areaID + "'";
if ((strsql != null) && (!strsql.Equals("")))
{
sql = sql + " and " + strsql;
}
sql = sql + " order by a.departmentID ";
Response.Write(sql);
SqlDataSource1.SelectCommand = sql;
} }
}
protected void Page_Load(object sender, EventArgs e)
{
//添加地区
if (!IsPostBack)
{
DataBase DB = new DataBase();
DataSet myDs2 = new DataSet();
string strsql = "";
string areaID = "";
string sql = "SELECT a.departmentID AS departmentID,a.name AS name,b.name as zkzbName, d.name AS hospitalName, b.sort as sortName,c.name AS provinceName FROM osw_department a ,osw_dictionary_zkzb b ,studyask_area c ,osw_hospital d where a.sort = b.sort and d.hospitalID=a.hospital and a.province=c.areaID and b.id=a.zkzb";
if ((Request.Params["areaID"] != null))
{
areaID = Request.Params["areaID"];
if (!strsql.Equals(""))
{
strsql = strsql + " and ";
}
strsql = strsql + "a.province='" + areaID + "'";
if ((strsql != null) && (!strsql.Equals("")))
{
sql = sql + " and " + strsql;
}
sql = sql + " order by a.departmentID ";
Response.Write(sql);
SqlDataSource1.SelectCommand = sql;
} }
}
在PageIndexChanging事件中这么写?
GridView1.PageIndex = e.NewPageIndex
GridView1.DataBind()
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind(); }