后台:
protected void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
SqlConnection conn=db.cn();
conn.Open();
SqlCommand cmd=new SqlCommand();
cmd.Connection=conn;
cmd.CommandText = "SELECT COUNT(*) FROM gg WHERE (title LIKE '%是%')";
ViewState["count"]=cmd.ExecuteScalar();
SqlDataAdapter da=new SqlDataAdapter();
DataSet ds=new DataSet();
int page=10;
page=page*(Convert.ToInt32(Request.Params["page"])-1);
if(page>0)
{
cmd.CommandText = "SELECT TOP 10 * FROM gg WHERE (id NOT IN (SELECT TOP " + page + " id FROM gg WHERE (title LIKE '%是%'))) and (title LIKE '%是%')";
}
else
{
cmd.CommandText="SELECT TOP 11 * FROM gg";
}
da.SelectCommand=cmd;
da.Fill(ds);
PagedDataSource objPage=new PagedDataSource();
objPage.DataSource=ds.Tables[0].DefaultView;
objPage.AllowPaging=true;
objPage.PageSize=10;
int CurPage;
if(Request.QueryString["Page"]!=null)
CurPage=Convert.ToInt32(Request.QueryString["Page"]);
else CurPage=1;
objPage.CurrentPageIndex=CurPage-1;
if(objPage.CurrentPageIndex%20==0)
{
Session["begin"]=objPage.CurrentPageIndex+1;
}
Label1.Text="当前页:第"+CurPage.ToString()+"页";
if(!objPage.IsFirstPage)
HyperLink1.NavigateUrl=Request.CurrentExecutionFilePath+ "?Page=" +Convert.ToString(CurPage-1);
if(!objPage.IsLastPage)
HyperLink2.NavigateUrl=Request.CurrentExecutionFilePath+ "?Page=" +Convert.ToString(CurPage+1);
DataList1.DataSource=ds;
DataList1.DataBind();
ds.Clear();
conn.Close();
}
}
public void GetString()
{
int i = Convert.ToInt32(Session["begin"]);
if (Convert.ToInt32(Request.Params["page"]) % 20 == 0 && Request.Params["page"] != null)
{
i = i - 20;
}
if ( Convert.ToInt32(Request.Params["page"])>i + 20)
{
i = i+20;
}
for(;i <= (Convert.ToInt32(ViewState["count"])+10)/10;i++)
{
if (i%20 == 1)
{
if (i == 1)
{ }
else
{
Response.Write("</TD> <a href='" + Request.CurrentExecutionFilePath + "?page=" + (i - 20) + "' alt=上20页>上20页</a> </TD>");
}
}
Response.Write("</TD> <a href='"+Request.CurrentExecutionFilePath+"?page="+i+"'>"+i+"</a> </TD>");
if(i%20==0)
{
i++;
Response.Write("</TD> <a href='" + Request.CurrentExecutionFilePath + "?page=" + i + "' alt=下20页>下20页</a> </TD>");
break;
}
}
}
前台:
<form id="Form1" method="post" runat="server">
<TABLE id="Table1" style="Z-INDEX: 101; LEFT: 40px; WIDTH: 704px; POSITION: absolute; TOP: 24px; HEIGHT: 163px"
cellSpacing="1" cellPadding="1" width="704" border="1">
<TR>
<TD style="WIDTH: 63px"><FONT face="宋体"></FONT></TD>
<TD style="WIDTH: 516px"><FONT face="宋体"><asp:datalist id="DataList1" runat="server">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"title").ToString() %>
</ItemTemplate>
<SeparatorTemplate>
......................
</SeparatorTemplate>
<AlternatingItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"title").ToString() %>
</AlternatingItemTemplate>
</asp:datalist></FONT></TD>
<TD><FONT face="宋体"></FONT></TD>
</TR>
<TR>
<TD style="WIDTH: 63px"><FONT face="宋体"><asp:hyperlink id="HyperLink1" runat="server">HyperLink</asp:hyperlink></FONT></TD>
<TD style="WIDTH: 516px">
<table>
<%GetString();%>
</table>
</TD>
<TD><FONT face="宋体"><asp:hyperlink id="HyperLink2" runat="server">HyperLink</asp:hyperlink><asp:label id="Label1" runat="server">当前第XXX页</asp:label></FONT></TD>
</TR>
</TABLE>
</form>