欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

动态生成table并实现分页效果心得分享

程序员文章站 2024-03-02 14:19:16
前台代码: 复制代码 代码如下:
搜索字:
前台代码:
复制代码 代码如下:

<table style="width: 100%;">
<tr>
<td>
搜索字: <asp:textbox id="textbox1" runat="server"></asp:textbox>
<asp:button id="button1" runat="server" text="搜索" onclick="button1_click" />
</td>
</tr>
<tr>
<td>
<div id="divmain" runat="server"> </div>
</td>
</tr>
<tr>
<td>
<asp:button id="btnfirst" runat="server" text="首页" onclick="btnfirst_click" />
<asp:button id="btnpro" runat="server" text="上一页" onclick="btnpro_click" />
<asp:button id="btnnext" runat="server" text="下一页" onclick="btnnext_click" />
<asp:button id="btnlast" runat="server" text="末页" onclick="btnlast_click" />
 <asp:label id="labcountpage" runat="server"></asp:label>
<asp:label id="labpage" runat="server"></asp:label>
<asp:textbox id="textbox2" runat="server" width="16px"></asp:textbox>
<asp:button id="btngo" runat="server" onclick="btngo_click" text="go" />
</td>
</tr>
</table>

后台代码:
复制代码 代码如下:

protected void page_load(object sender, eventargs e)
{
if (!ispostback)
{
viewstate["pageindex"] = 1;
datapage();
}
}
private void datapage()
{
string constr = @"data source=.;initial catalog=news;user id=sa;password=111111;";
sqlconnection conn = new sqlconnection(constr);
conn.open();
sqlcommand cmd = conn.createcommand();
#region 用存储过程实现数据分页效果
//cmd.commandtype = commandtype.storedprocedure;
//cmd.commandtext = "pro_selectnewspage";
//cmd.parameters.addwithvalue("@pageindex", convert.toint32(viewstate["pageindex"]));
//cmd.parameters.addwithvalue("@pagesize", 3);
//sqlparameter parm = new sqlparameter("@count", sqldbtype.int);
//cmd.parameters.add(parm);
//parm.direction = parameterdirection.output;
#endregion
#region 传递sql语句实现分页并对搜索实现分页默认显示两条记录
cmd.commandtype = commandtype.text;
cmd.commandtext = "select top 2 * from (select * from t_news where newstitle like @title) as t where t.id not in(select top ((@pageindex-1)*2) t.id from (select * from t_news where newstitle like @title) as t) order by t.id";
cmd.parameters.addwithvalue("@pageindex", convert.toint32(viewstate["pageindex"]));
cmd.parameters.addwithvalue("@title","%"+textbox1.text.trim()+"%");
#endregion
sqldataadapter adapter = new sqldataadapter(cmd);
datatable dt = new datatable();
adapter.fill(dt);
//viewstate["pagecount"] = parm.value;
cmd.commandtext = "select count(*) from t_news where newstitle like @titles";
cmd.parameters.addwithvalue("@titles", "%" + textbox1.text.trim() + "%");
int sum = convert.toint32(cmd.executescalar());
if (sum % 2 == 0)
{
viewstate["pagecount"] = sum/2;
}
else
{
viewstate["pagecount"] = sum / 2 + 1;
}
conn.close();
conn.dispose();
stringbuilder sb = new stringbuilder();
sb.append("<table>");
foreach (datarow row in dt.rows)
{
sb.append("<tr><td>"+row["newstitle"].tostring()+"</td>");
sb.append("<td>" + row["newscontent"].tostring() + "</td>");
sb.append("<td>" + row["createtime"].tostring() + "</td></tr>");
}
sb.append("</table>");
divmain.innerhtml = sb.tostring();
labcountpage.text = "总共" + viewstate["pagecount"] + "页";
labpage.text = "当前第" + viewstate["pageindex"] + "页";
}
protected void button1_click(object sender, eventargs e)
{
viewstate["pageindex"] = "1";
datapage();
}
protected void btnfirst_click(object sender, eventargs e)
{
viewstate["pageindex"] = "1";
datapage();
}
protected void btnpro_click(object sender, eventargs e)
{
int index = convert.toint32(viewstate["pageindex"]);
index--;
if (index >= 1)
{
viewstate["pageindex"] = index;
datapage();
}
}
protected void btnnext_click(object sender, eventargs e)
{
int index = convert.toint32(viewstate["pageindex"]);
index++;
if (index <= convert.toint32(viewstate["pagecount"]))
{
viewstate["pageindex"] = index;
datapage();
}
}
protected void btnlast_click(object sender, eventargs e)
{
viewstate["pageindex"] =viewstate["pagecount"];
datapage();
}
protected void btngo_click(object sender, eventargs e)
{
int i=convert.toint32(textbox2.text);
if (i > 0 && i <= convert.toint32(viewstate["pagecount"]))
{
viewstate["pageindex"] = i;
datapage();
}
}