Aspx:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%" GridLines="None" CellSpacing="2" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" DataKeyNames="ItemId">
<Columns>
<asp:TemplateField HeaderText="编号">
<ItemTemplate ><%# GetCount() %></ItemTemplate>
<HeaderStyle Width="5%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="场馆标志">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("ImageUrl") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image1" runat="server"ImageUrl='<%#"~/Upfile/" + Eval("ImageUrl") %>' Width="30" Height="28"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ReportTitle" HeaderText="场地名称" />
<asp:BoundField DataField="LinkMan" HeaderText="联系人" />
<asp:BoundField DataField="CreatedDate" HeaderText="发布日期" DataFormatString="{0:yyyy-MM-dd}" HtmlEncode="False" />
<asp:BoundField DataField="ReportArea" HeaderText="面积" />
<asp:TemplateField HeaderText="展览计划" ShowHeader="False">
<ItemStyle BorderWidth="0px"Wrap="False"/>
<ItemTemplate>
<asp:LinkButton ID="LinkButton2"runat="server"CausesValidation="false"CommandName="Select"
Text='<%#DataBinder.Eval(Container,"DataItem.ReportExprision").ToString().Length > 10 ?DataBinder.Eval(Container,"DataItem.ReportExprision").ToString().Substring(0, 10) +"..." : DataBinder.Eval(Container,"DataItem.ReportExprision")%>'></asp:LinkButton>
<%--<asp:LinkButton ID="LinkButton2"runat="server" CausesValidation="false"CommandName="Select"
Text='<%# DataBinder.Eval(Container,"DataItem.ProjectName").ToString().Length > 10 ?DataBinder.Eval(Container,"DataItem.ProjectExprision").ToString().Substring(0, 10) +"..." : DataBinder.Eval(Container,"DataItem.ProjectExprision")%>'></asp:LinkButton> --%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="网站链接" ShowHeader="False">
<ItemStyle BorderWidth="0px"Wrap="False"/>
<ItemTemplate>
<asp:HyperLink ID="hlLink" runat="server"NavigateUrl='<%#DataBinder.Eval(Container, "DataItem.ReportWeb")%>'><%# DataBinder.Eval(Container,"DataItem.ReportWeb")%></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle Font-Size="13px" HorizontalAlign="Center" />
<HeaderStyle Font-Size="14px" HorizontalAlign="Center" />
</asp:GridView>
<table style="width:100%;">
<tr id="pagerTr" runat="server">
<td align="right" style="font-size: 12px; height: 28px">
<asp:Label ID="labTotal" runat="server" Text="共0条记录 第0页/共0页"></asp:Label>
<asp:LinkButton ID="linkFirst" runat="server" OnClick="linkFirst_Click">首页</asp:LinkButton>
<asp:LinkButton ID="linkPrevious" runat="server" OnClick="linkPrevious_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="linkNext" runat="server" OnClick="linkNext_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="linkLast" runat="server" OnClick="linkLast_Click">尾页</asp:LinkButton>
<asp:TextBox ID="txtRows"runat="server"Width="20px"></asp:TextBox>
<asp:LinkButton ID="linkGO" runat="server"CausesValidation="False"OnClick="linkGO_Click">GO</asp:LinkButton>
</td>
</tr>
</table>
.CS:
#region Private Members
publicint count=0;
ReportControllerrc = new ReportController();
introwcount = 10; //要显示的行数
#endregion
protectedvoid Page_Load(objectsender, EventArgs e)
{
try
{
if (!Page.IsPostBack)
{
ViewState[this.ID + "startPage"]= 1;
ViewState[this.ID + "totalPage"]= 0;
Binder();
}
}
catch(Exception exc) //Modulefailed to load
{
Exceptions.ProcessModuleLoadException(this, exc);
}
}
///<summary>
///首页
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
protectedvoid linkFirst_Click(objectsender, EventArgs e)
{
ViewState[this.ID + "startPage"]= 1;
Binder();
}
///<summary>
///上一页
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
protectedvoid linkPrevious_Click(object sender, EventArgs e)
{
intstartPage = int.Parse(ViewState[this.ID + "startPage"].ToString());
startPage = startPage -1;
startPage = startPage< 1 ? 1 : startPage;
ViewState[this.ID + "startPage"]= startPage;
Binder();
}
///<summary>
///下一页
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
protectedvoid linkNext_Click(objectsender, EventArgs e)
{
intstartPage = int.Parse(ViewState[this.ID + "startPage"].ToString());
inttotalPage = int.Parse(ViewState[this.ID + "totalPage"].ToString());
startPage = startPage +1;
startPage = startPage> totalPage ? totalPage : startPage;
ViewState[this.ID + "startPage"]= startPage;
Binder();
}
///<summary>
///尾页
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
protectedvoid linkLast_Click(objectsender, EventArgs e)
{
ViewState[this.ID + "startPage"]= ViewState[this.ID + "totalPage"];
Binder();
}
///<summary>
///跳转指定页
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
protectedvoid linkGO_Click(objectsender, EventArgs e)
{
intcurPage = 0;
try
{
curPage = int.Parse(this.txtRows.Text.Trim());
}
catch
{
this.txtRows.Text = "";
Page.ClientScript.RegisterStartupScript(this.GetType(),"prompt", "alert('输入的页码不正确,请重新输入!')", true);
return;
}
inttotalPage = int.Parse(ViewState[this.ID + "totalPage"].ToString());
curPage = curPage >totalPage ? totalPage : curPage;
curPage = curPage < 1? 1 : curPage;
ViewState[this.ID + "startPage"]= curPage;
Binder();
}
///<summary>
///搜索
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
protectedvoid linkSearch_Click(objectsender, EventArgs e)
{
try
{
Binder();
}
catch
{
Page.ClientScript.RegisterStartupScript(this.GetType(),"prompt", "alert('查询失败,请稍后再试!')", true);
}
}
///<summary>
///数据绑定
///</summary>
publicvoid Binder()
{
intstart = (int.Parse(ViewState[this.ID + "startPage"].ToString())- 1) * rowcount;
inttotalCount = 0;
stringtitle = this.txtSearch.Text.Trim();
List<ReportInfo> rInfo = rc.GetQueryReports(start,rowcount, title, out totalCount);
this.GridView1.DataSource= rInfo;
this.GridView1.DataBind();
txtRows.Text = "";
ViewState[this.ID + "totalPage"]= (totalCount / rowcount) + ((totalCount % rowcount) > 0 ? 1 : 0);
labTotal.Text = "共" + totalCount.ToString() + "条记录 第" + ViewState[this.ID+ "startPage"].ToString() + "页/共" + ViewState[this.ID + "totalPage"].ToString() + "页";
}
///<summary>
///查看
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
protectedvoid GridView1_SelectedIndexChanged(object sender, EventArgse)
{
intitemid = int.Parse(this.GridView1.DataKeys[this.GridView1.SelectedIndex].Value.ToString());
stringurl = ModulePath + "ViewInfo.aspx?ItemId="+ itemid;
stringscript = "<scriptlanguage='javascript'>";
script += "window.showModalDialog('" + url + "',window,'DialogHeight=600px;dialogWidth:800px;status:no;scrollbars:auto;Resizable=no;');";
script += "</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(),"", script);
}
publicint GetCount()
{
count = count + 1;
intviewindex = int.Parse(ViewState[this.ID + "startPage"].ToString());
return (viewindex-1)* rowcount + count;
}
重点:
public int GetCount()
{
count = count + 1;
intviewindex = int.Parse(ViewState[this.ID + "startPage"].ToString());
return (viewindex-1)* rowcount + count;
}
其中ViewState[this.ID+ "startPage"]表示当前页的索引,比如1就表示第一页。Rowcount表示每页显示的行数(也就是多少条数据)。Count就是编码了,每页都是从1开始的。