int pagesize = 3;
//int pagelastindex = 0;
//int pageindex = 1;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)//页面第一次加载
{
//ViewState虽然是声明在函数内部,看似是局部变量,但是在类中的其他函数中也可以直接使用
ViewState["pageindex"] = 1;//让第一次加载显示第一页
GetCount();//得到记录条数
LoadData();//加载
}
}
private void GetCount()//查询总记录条数
{
string strcon = @"Data Source=YHB-PC;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=yhb@163";
SqlConnection conn = new SqlConnection(strcon);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT COUNT(*) FROM T_News WHERE NewsTitle LIKE @newskey OR NewsContent LIKE @newskey";//这样写上条件要想不用此功能就可以不在文本框写内容
cmd.Parameters.AddWithValue("@newskey","%"+txtKey.Text+"%");
conn.Open();
int totalcount = Convert.ToInt32(cmd.ExecuteScalar());
if (totalcount % pagesize == 0)
{
ViewState["pagelastindex"] = totalcount / pagesize;//查询页数
}
else
{
ViewState["pagelastindex"] = totalcount / pagesize + 1;
}
}
private void LoadData()
{
string strcon = @"Data Source=YHB-PC;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=yhb@163";
SqlConnection conn = new SqlConnection(strcon);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT TOP(@pagesize) * FROM T_News WHERE(NewsTitle LIKE @newskey OR NewsContent LIKE @newskey) AND Id NOT IN(SELECT TOP ((@pageindex-1)*@pagesize) Id FROM T_News WHERE NewsTitle LIKE @newskey OR NewsContent LIKE @newskey ORDER BY Id )ORDER BY Id";
cmd.Parameters.AddWithValue("@pageindex", Convert.ToInt32(ViewState["pageindex"]));
cmd.Parameters.AddWithValue("@pagesize", pagesize);
cmd.Parameters.AddWithValue("@newskey","%"+txtKey.Text+"%");
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
cmd.Dispose();
conn.Dispose();
#region 字符串拼接
StringBuilder sb1 = new StringBuilder();
sb1.Append("<table border=2>");
sb1.Append("<tr><td>标题</td><td>内容</td><td>创建时间</td></tr>");
for (int i = 0; i < dt.Rows.Count; i++)
{
sb1.Append("<tr>");
sb1.Append("<td>" + dt.Rows[i]["NewsTitle"].ToString() + "</td>");
sb1.Append("<td>" + dt.Rows[i]["NewsContent"].ToString() + "</td>");
sb1.Append("<td>" + dt.Rows[i]["CreateTime"].ToString() + "</td>");
sb1.Append("</tr>");
}
sb1.Append("</table>");
#endregion
divResult.InnerHtml = sb1.ToString();
}
protected void btnQuery_Click(object sender, ImageClickEventArgs e)//匹配按钮
{
ViewState["pageindex"] = 1;
GetCount();
LoadData();
}
protected void btnFirst_Click(object sender, EventArgs e)
{
ViewState["pageindex"] = 1;
LoadData();
}
protected void btnPre_Click(object sender, EventArgs e)
{
int pageindex = Convert.ToInt32(ViewState["pageindex"]);
if (pageindex > 1)
{
pageindex--;
ViewState["pageindex"] = pageindex;
//放到里边,当pageindex=1的时候,也就是当已经到达第一页的时候,用户如果重复点击“上一页”,不会再次去数据库里查询第一页的数据,而是使用ViewState中存储的第一页的数据
LoadData();
}
}
//下一页
protected void btnNext_Click(object sender, EventArgs e)
{
int pageindex = Convert.ToInt32(ViewState["pageindex"]);
if (pageindex < Convert.ToInt32(ViewState["pagelastindex"]))
{
pageindex++;
ViewState["pageindex"] = pageindex;
LoadData();
}
}
protected void btnLast_Click(object sender, EventArgs e)
{
ViewState["pageindex"] = ViewState["pagelastindex"];
LoadData();
}