这个分页的主要是用了四个linkbutton按纽的事件,然后利用后台传递的条件调用分页处理的方法,在这个控件中可以点击表头排序,并且可以突出显示关键字等。
显示的页面:
< table border = " 0 " cellpadding = " 0 " cellspacing = " 0 " style = " width: 100%; height: 466px; " >
< tr >
< td style = " vertical-align: top; width: 100%; height: 233px; text-align: center; " >
< asp:GridView ID = " GridViewFileList " runat = " server " AllowPaging = " True " AllowSorting = " True "
AutoGenerateColumns = " False " CellPadding = " 4 " GridLines = " None " PageSize = " 20 " Width = " 98% " OnRowDataBound = " GridViewFileList_RowDataBound " OnSorting = " GridViewFileList_Sorting " >
< Columns >
< asp:BoundField DataField = " FileID " Visible = " False " >
< ItemStyle Height = " 26px " />
< HeaderStyle Height = " 26px " />
</ asp:BoundField >
< asp:TemplateField HeaderText = " 文件名 " SortExpression = " title " >
< HeaderStyle CssClass = " gridheadshowdata " />
< ItemTemplate >
< asp:HyperLink ID = " FileName " runat = " server " Target = " _blank " NavigateUrl = ' <%# "FileDetail.aspx?FileID="+Eval("FileID")%> ' CssClass = " griditemshowdata "
Text = ' <%# Eval("title") %> ' ToolTip = ' <%# Eval("FileDesc") %> ' ></ asp:HyperLink >
</ ItemTemplate >
< ItemStyle HorizontalAlign = " Left " />
</ asp:TemplateField >
< asp:BoundField DataField = " FileTypeName " HeaderText = " 类别 " SortExpression = " FileTypeID " HtmlEncode = " False " >
< ItemStyle Height = " 26px " CssClass = " griditemshowdata " HorizontalAlign = " Center " />
< HeaderStyle CssClass = " gridheadshowdata " Height = " 26px " />
</ asp:BoundField >
< asp:BoundField DataField = " GradeName " HeaderText = " 年级 " SortExpression = " GradeID " HtmlEncode = " False " >
< ItemStyle CssClass = " griditemshowdata " HorizontalAlign = " Center " />
< HeaderStyle CssClass = " gridheadshowdata " />
</ asp:BoundField >
< asp:BoundField DataField = " FileSize " HeaderText = " 文件大小 " SortExpression = " FileSize2 " HtmlEncode = " False " >
< ItemStyle CssClass = " griditemshowdata " HorizontalAlign = " Center " />
< HeaderStyle CssClass = " gridheadshowdata " />
</ asp:BoundField >
< asp:BoundField DataField = " AddDate " DataFormatString = " {0:d} " HeaderText = " 发布时间<font color='#FF0000'>↓</font> " HtmlEncode = " False "
SortExpression = " AddDate " >
< ItemStyle CssClass = " griditemshowdata " Height = " 26px " HorizontalAlign = " Center " />
< HeaderStyle CssClass = " gridheadshowdata " Height = " 26px " />
</ asp:BoundField >
< asp:BoundField DataField = " hit " HeaderText = " 下载次数 " SortExpression = " hit " HtmlEncode = " False " >
< ItemStyle CssClass = " griditemshowdata " Height = " 26px " HorizontalAlign = " Center " />
< HeaderStyle CssClass = " gridheadshowdata " Height = " 26px " />
</ asp:BoundField >
< asp:TemplateField >
< HeaderTemplate >
下载
</ HeaderTemplate >
< ItemStyle CssClass = " griditemshowdata " Height = " 26px " HorizontalAlign = " Center " />
< HeaderStyle CssClass = " gridheadshowdata " Height = " 26px " />
< ItemTemplate >
< a href = " GetDownloadFile.aspx?FileID=<%#Eval( " FileID " ) %>&AddDate=<%#Eval( " AddDate " ) %>&UpFile=<%#Eval( " UpFile " ) %> " >
<%-- < a href = ' <%#Eval("DownloadPath") %> ' >--%>
< asp:Image ID = " Image2 " runat = " server " ImageUrl = " ~/images/icon_down.gif " /></ a >
</ ItemTemplate >
</ asp:TemplateField >
</ Columns >
< RowStyle BackColor = " #EFEFEF " />
< EmptyDataTemplate >
< table border = ' 0 ' cellpadding = ' 0 ' cellspacing = ' 0 ' style = ' border-right: coral 1px dotted;border-top: coral 1px dotted; border-left: coral 1px dotted; border-bottom: coral 1px dotted;background-color: #ffffff;width:100%;height:55px; ' >< tr >< td align = ' center ' >< font color = ' coral ' >< b > 对不起,没有搜索到相关的记录,我们将尽快上传相关数据 < b ></ font ></ td ></ tr ></ table >
</ EmptyDataTemplate >
< HeaderStyle BackColor = " #D5E4F4 " Font - Bold = " True " />
< AlternatingRowStyle BackColor = " White " />
</ asp:GridView >
< asp:Panel ID = " PanelPageTable " runat = " server " Height = " 28px " Visible = " False " Width = " 98% " >
< table border = " 0 " cellpadding = " 0 " cellspacing = " 0 " style = " width: 100%; height: 21px " >
< tr >
< td style = " width: 100px; height: 13px " >
</ td >
< td style = " width: 98px; height: 13px " >
< div style = " width: 212px; height: 7px; font-size:14px; " >
总共 <%= RecordCount %> 条 第
<%= PageIndex %>/<%= PageCount %>
页
</ div >
</ td >
< td style = " width: 97px; height: 13px " >
< div style = " width: 308px; height: 8px; text-align: right; font-size:14px; " >
< asp:LinkButton ID = " FirstPage " runat = " server " CommandArgument = " First " OnClick = " FirstPage_Click " > 第一页 </ asp:LinkButton >
< asp:LinkButton ID = " PreviousPage " runat = " server " CommandArgument = " Prev " OnClick = " PreviousPage_Click " > 上一页 </ asp:LinkButton >
< asp:LinkButton ID = " NextPage " runat = " server " CommandArgument = " Next " OnClick = " NextPage_Click " > 下一页 </ asp:LinkButton >
< asp:LinkButton ID = " LastPage " runat = " server " CommandArgument = " Last " OnClick = " LastPage_Click " > 最后一页 </ asp:LinkButton >
< asp:TextBox ID = " TextGotoPage " runat = " server " Font - Size = " 16px " CssClass = " pageeditbox " Width = " 22px " Height = " 20px " ></ asp:TextBox >
< asp:Button ID = " PageGo " Font - Size = " 16px " causesvalidation = " False " commandargument = " -1 " commandname = " Page " runat = " server " CssClass = " pagebutton " Text = " Go " OnClick = " PageGo_Click " Height = " 23px " Width = " 28px " />
</ div >
</ td >
< td style = " width: 80px; height: 13px " >
< div style = " width: 4px; height: 1px " >
</ div >
</ td >
</ tr >
</ table >
</ asp:Panel >
< asp:HiddenField ID = " HiddenFieldWhereCondition " runat = " server " />
< asp:HiddenField ID = " HiddenFieldPageIndex " runat = " server " />
< asp:HiddenField ID = " HiddenFieldPageCount " runat = " server " />
< asp:HiddenField ID = " HiddenFieldRecordCount " runat = " server " />
< asp:HiddenField ID = " HiddenFieldGridViewSortExpression " runat = " server " />
< asp:HiddenField ID = " HiddenFieldGridViewSortDirection " runat = " server " />
< asp:HiddenField ID = " HiddenKeyWord " runat = " server " />
</ td ></ tr >
</ table >
后台的CS代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Bussiness;
public partial class DataShow : System.Web.UI.UserControl
{
public string GridViewSortExpression = "AddDate";
public string GridViewSortDirection = "DESC";
public int RecordCount = 0;
public int PageIndex = 0;
public int PageCount = 0;
public string WhereCondition = string.Empty;
FilesBussiness files = new FilesBussiness();
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
//this.PageGo.Attributes.Add("onclick", "if(1){alert(((document.all('ctl00$ContentPlaceHolderSearch$MyDataList1$TextGotoPage')).value);document.all('ctl00$ContentPlaceHolderSearch$MyDataList1$TextGotoPage').focus();return false;}");
}
public void SetSortExpress(string expression, string direction)
{
GridViewSortExpression = expression;
GridViewSortDirection = direction;
this.HiddenFieldGridViewSortExpression.Value = expression;
this.HiddenFieldGridViewSortDirection.Value = direction;
}
public void SetAllowSort(bool flag)
{
this.GridViewFileList.AllowSorting =flag;
}
public void BindDataSource(string WhereCondition,string KeyWord)
{
RecordCount = files.SelectPageFilesCount(WhereCondition);
this.HiddenKeyWord.Value = KeyWord;
PageIndex = 1;
if (RecordCount % this.GridViewFileList.PageSize > 0)
PageCount = RecordCount / this.GridViewFileList.PageSize + 1;
else
PageCount = RecordCount / this.GridViewFileList.PageSize;
dt = files.SelectPageFiles(WhereCondition, 1, this.GridViewFileList.PageSize, GridViewSortExpression + " " + GridViewSortDirection);
this.GridViewFileList.DataSource = dt;
this.GridViewFileList.DataBind();
if (RecordCount > 0)
{
this.PanelPageTable.Visible = true;
}
else
{
this.PanelPageTable.Visible = false;
}
if (!GridViewSortExpression.Equals("AddDate"))
{
changeHeaders(this.GridViewFileList.HeaderRow);
this.GridViewFileList.DataBind();
}
this.HiddenFieldWhereCondition.Value = WhereCondition;
this.HiddenFieldGridViewSortExpression.Value = GridViewSortExpression;
this.HiddenFieldGridViewSortDirection.Value = GridViewSortDirection;
this.HiddenFieldPageIndex.Value = PageIndex.ToString();
this.HiddenFieldRecordCount.Value = RecordCount.ToString();
this.HiddenFieldPageCount.Value = PageCount.ToString();
this.TextGotoPage.Text = PageIndex.ToString();
}
protected void FirstPage_Click(object sender, EventArgs e)
{
GridViewSortExpression = this.HiddenFieldGridViewSortExpression.Value.Trim();
GridViewSortDirection = this.HiddenFieldGridViewSortDirection.Value.Trim();
PageIndex = Int32.Parse(this.HiddenFieldPageIndex.Value.Trim());
WhereCondition = this.HiddenFieldWhereCondition.Value;
PageIndex = 1;
RecordCount = files.SelectPageFilesCount(WhereCondition);
if (RecordCount % this.GridViewFileList.PageSize > 0)
PageCount = RecordCount / this.GridViewFileList.PageSize + 1;
else
PageCount = RecordCount / this.GridViewFileList.PageSize;
this.TextGotoPage.Text = Convert.ToString(PageIndex);
this.GridViewFileList.DataSource = files.SelectPageFiles(WhereCondition, PageIndex, this.GridViewFileList.PageSize, GridViewSortExpression + " " + GridViewSortDirection);
this.GridViewFileList.DataBind();
changeHeaders(this.GridViewFileList.HeaderRow);
this.GridViewFileList.DataBind();
this.HiddenFieldPageIndex.Value = PageIndex.ToString();
this.HiddenFieldRecordCount.Value = RecordCount.ToString();
this.HiddenFieldPageCount.Value = PageCount.ToString();
if (RecordCount > 0)
{
this.PanelPageTable.Visible = true;
}
else
{
this.PanelPageTable.Visible = false;
}
}
protected void PreviousPage_Click(object sender, EventArgs e)
{
GridViewSortExpression = this.HiddenFieldGridViewSortExpression.Value.Trim();
GridViewSortDirection = this.HiddenFieldGridViewSortDirection.Value.Trim();
PageIndex = Int32.Parse(this.HiddenFieldPageIndex.Value.Trim());
WhereCondition = this.HiddenFieldWhereCondition.Value;
RecordCount = files.SelectPageFilesCount(WhereCondition);
if (RecordCount % this.GridViewFileList.PageSize > 0)
PageCount = RecordCount / this.GridViewFileList.PageSize + 1;
else
PageCount = RecordCount / this.GridViewFileList.PageSize;
PageIndex = (PageIndex - 1) <= 0 ? 1 : PageIndex - 1;
this.TextGotoPage.Text = Convert.ToString(PageIndex);
this.GridViewFileList.DataSource = files.SelectPageFiles(WhereCondition, PageIndex, this.GridViewFileList.PageSize, GridViewSortExpression + " " + GridViewSortDirection);
this.GridViewFileList.DataBind();
changeHeaders(this.GridViewFileList.HeaderRow);
this.GridViewFileList.DataBind();
this.HiddenFieldPageIndex.Value = PageIndex.ToString();
this.HiddenFieldRecordCount.Value = RecordCount.ToString();
this.HiddenFieldPageCount.Value = PageCount.ToString();
if (RecordCount > 0)
{
this.PanelPageTable.Visible = true;
}
else
{
this.PanelPageTable.Visible = false;
}
}
protected void NextPage_Click(object sender, EventArgs e)
{
GridViewSortExpression = this.HiddenFieldGridViewSortExpression.Value.Trim();
GridViewSortDirection = this.HiddenFieldGridViewSortDirection.Value.Trim();
PageIndex = Int32.Parse(this.HiddenFieldPageIndex.Value.Trim());
WhereCondition = this.HiddenFieldWhereCondition.Value;
RecordCount = files.SelectPageFilesCount(WhereCondition);
if (RecordCount % this.GridViewFileList.PageSize > 0)
PageCount = RecordCount / this.GridViewFileList.PageSize + 1;
else
PageCount = RecordCount / this.GridViewFileList.PageSize;
PageIndex = (PageIndex + 1) >= PageCount ? PageCount : PageIndex + 1;
this.TextGotoPage.Text = Convert.ToString(PageIndex);
this.GridViewFileList.DataSource = files.SelectPageFiles(WhereCondition, PageIndex, this.GridViewFileList.PageSize, GridViewSortExpression + " " + GridViewSortDirection);
this.GridViewFileList.DataBind();
changeHeaders(this.GridViewFileList.HeaderRow);
this.GridViewFileList.DataBind();
this.HiddenFieldPageIndex.Value = PageIndex.ToString();
this.HiddenFieldRecordCount.Value = RecordCount.ToString();
this.HiddenFieldPageCount.Value = PageCount.ToString();
if (RecordCount > 0)
{
this.PanelPageTable.Visible = true;
}
else
{
this.PanelPageTable.Visible = false;
}
}
protected void LastPage_Click(object sender, EventArgs e)
{
GridViewSortExpression = this.HiddenFieldGridViewSortExpression.Value.Trim();
GridViewSortDirection = this.HiddenFieldGridViewSortDirection.Value.Trim();
PageIndex = Int32.Parse(this.HiddenFieldPageIndex.Value.Trim());
WhereCondition = this.HiddenFieldWhereCondition.Value;
RecordCount = files.SelectPageFilesCount(WhereCondition);
if (RecordCount % this.GridViewFileList.PageSize > 0)
PageCount = RecordCount / this.GridViewFileList.PageSize + 1;
else
PageCount = RecordCount / this.GridViewFileList.PageSize;
PageIndex = PageCount;
this.TextGotoPage.Text = Convert.ToString(PageIndex);
this.GridViewFileList.DataSource = files.SelectPageFiles(WhereCondition, PageIndex, this.GridViewFileList.PageSize, GridViewSortExpression + " " + GridViewSortDirection);
this.GridViewFileList.DataBind();
changeHeaders(this.GridViewFileList.HeaderRow);
this.GridViewFileList.DataBind();
this.HiddenFieldPageIndex.Value = PageIndex.ToString();
this.HiddenFieldRecordCount.Value = RecordCount.ToString();
this.HiddenFieldPageCount.Value = PageCount.ToString();
if (RecordCount > 0)
{
this.PanelPageTable.Visible = true;
}
else
{
this.PanelPageTable.Visible = false;
}
}
protected void PageGo_Click(object sender, EventArgs e)
{
GridViewSortExpression = this.HiddenFieldGridViewSortExpression.Value.Trim();
GridViewSortDirection = this.HiddenFieldGridViewSortDirection.Value.Trim();
PageIndex = Int32.Parse(this.HiddenFieldPageIndex.Value.Trim());
ValidateBussiness v = new ValidateBussiness();
if (this.TextGotoPage.Text.Trim() == "")
this.TextGotoPage.Text = "1";
if (!v.Number(this.TextGotoPage.Text.Trim()))
FilesBussiness.MessageBox(Response, "输入不正确", true);
else
{
WhereCondition = this.HiddenFieldWhereCondition.Value;
RecordCount = files.SelectPageFilesCount(WhereCondition);
if (RecordCount % this.GridViewFileList.PageSize > 0)
PageCount = RecordCount / this.GridViewFileList.PageSize + 1;
else
PageCount = RecordCount / this.GridViewFileList.PageSize;
PageIndex = Int32.Parse(this.TextGotoPage.Text.Trim());
if (PageIndex > PageCount)
PageIndex = PageCount;
else if (PageIndex <= 0)
PageIndex = 1;
this.TextGotoPage.Text = Convert.ToString(PageIndex);
this.GridViewFileList.DataSource = files.SelectPageFiles(WhereCondition, PageIndex, this.GridViewFileList.PageSize, GridViewSortExpression + " " + GridViewSortDirection);
this.GridViewFileList.DataBind();
changeHeaders(this.GridViewFileList.HeaderRow);
this.GridViewFileList.DataBind();
this.HiddenFieldPageIndex.Value = PageIndex.ToString();
this.HiddenFieldRecordCount.Value = RecordCount.ToString();
this.HiddenFieldPageCount.Value = PageCount.ToString();
if (RecordCount > 0)
{
this.PanelPageTable.Visible = true;
}
else
{
this.PanelPageTable.Visible = false;
}
}
}
protected void GridViewFileList_RowDataBound(object sender, GridViewRowEventArgs e)
{
ArrayList list = new ArrayList();
if (this.HiddenKeyWord .Value .Trim()!= "")
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string temp = ((HyperLink)e.Row.FindControl("FileName")).Text; //((System.Data.DataRowView)(e.Row.DataItem)).Row.ItemArray[1].ToString();
list = files.SplitString(this.HiddenKeyWord.Value.Trim(), " ");
int length = temp.Length;
for (int i = 0; i < list.Count; i++)
{
if (temp.IndexOf(list[i].ToString()) >= 0)
{
((HyperLink)e.Row.FindControl("FileName")).Text = temp.Substring(0, temp.IndexOf(list[i].ToString()));
((HyperLink)e.Row.FindControl("FileName")).Text += "<font color=red>" + list[i].ToString() + "</font>";
((HyperLink)e.Row.FindControl("FileName")).Text += temp.Substring(temp.IndexOf(list[i].ToString()) + list[i].ToString().Length);
temp = ((HyperLink)e.Row.FindControl("FileName")).Text;
length = temp.Length;
}
}
}
}
}
/// <summary>
/// 为GridView.HeaderRow添加排序标识符
/// </summary>
/// <param name="headerRow">GridViewRow</param>
private void changeHeaders(GridViewRow headerRow)
{
if (this.GridViewFileList.Rows.Count > 0)
{
for (int i = 0; i < headerRow.Cells.Count; i++)
{
if (headerRow.Cells[i] is DataControlFieldCell)
{
DataControlField field = ((DataControlFieldCell)headerRow.Cells[i]).ContainingField;
if (field.HeaderText.IndexOf("↓") >= 0 || field.HeaderText.IndexOf("↑") >= 0)
field.HeaderText = field.HeaderText.Substring(0, field.HeaderText.Length - "<font color='#FF0000'>↓</font>".Length); //把开始的排序方式去掉
if (this.HiddenFieldGridViewSortExpression.Value != "" && this.HiddenFieldGridViewSortExpression.Value.Trim().Equals(field.SortExpression.Trim()))
{
if (this.HiddenFieldGridViewSortDirection.Value.Equals("DESC")) //增加新的排序方式
field.HeaderText = field.HeaderText + "<font color='#FF0000'>↓</font>";
else
field.HeaderText = field.HeaderText + "<font color='#FF0000'>↑</font>";
}
}
}
}
}
/// <summary>
/// GridView排序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridViewFileList_Sorting(object sender, GridViewSortEventArgs e)
{
GridViewSortExpression = this.HiddenFieldGridViewSortExpression.Value.Trim();
GridViewSortDirection = this.HiddenFieldGridViewSortDirection.Value.Trim();
PageIndex = Int32.Parse(this.HiddenFieldPageIndex.Value.Trim());
if (GridViewSortExpression.Equals(e.SortExpression))
{
GridViewSortExpression = e.SortExpression;
if (GridViewSortDirection.Equals("ASC"))
{
GridViewSortDirection = "DESC";
this.HiddenFieldGridViewSortExpression.Value = GridViewSortExpression;
//this.GridViewFileList.DataSource =files.SelectPageFiles(this.HiddenFieldWhereCondition.Value, PageIndex, this.GridViewFileList.PageSize, GridViewSortExpression + " " + GridViewSortDirection);
}
else if (GridViewSortDirection.Equals("DESC"))
{
GridViewSortDirection = "ASC";
this.HiddenFieldGridViewSortExpression.Value = GridViewSortExpression;
//this.GridViewFileList.DataSource = files.SelectPageFiles(this.HiddenFieldWhereCondition.Value, PageIndex, this.GridViewFileList.PageSize, GridViewSortExpression + " " + GridViewSortDirection);
}
else
{
GridViewSortDirection = "DESC";
this.HiddenFieldGridViewSortExpression.Value = GridViewSortExpression;
//this.GridViewFileList.DataSource = files.SelectPageFiles(this.HiddenFieldWhereCondition.Value, PageIndex, this.GridViewFileList.PageSize, GridViewSortExpression + " " + GridViewSortDirection);
}
}
else
{
GridViewSortDirection = "DESC";
GridViewSortExpression = e.SortExpression;
this.HiddenFieldGridViewSortExpression.Value = GridViewSortExpression;
}
this.HiddenFieldGridViewSortDirection.Value = GridViewSortDirection;
RecordCount = files.SelectPageFilesCount(this.HiddenFieldWhereCondition.Value);
if (RecordCount % this.GridViewFileList.PageSize > 0)
PageCount = RecordCount / this.GridViewFileList.PageSize + 1;
else
PageCount = RecordCount / this.GridViewFileList.PageSize;
this.GridViewFileList.DataSource = files.SelectPageFiles(this.HiddenFieldWhereCondition.Value, PageIndex, this.GridViewFileList.PageSize, GridViewSortExpression + " " + GridViewSortDirection);
this.GridViewFileList.DataBind();
changeHeaders(this.GridViewFileList.HeaderRow);
this.GridViewFileList.DataBind();
this.HiddenFieldPageIndex.Value = PageIndex.ToString();
this.HiddenFieldRecordCount.Value = RecordCount.ToString();
this.HiddenFieldPageCount.Value = PageCount.ToString();
if (RecordCount > 0)
{
this.PanelPageTable.Visible = true;
}
else
{
this.PanelPageTable.Visible = false;
}
}
}
调用的函数:
public DataTable SelectPageFiles(string WhereCondition, int PageIndex, int PageCount, string OrderCondition)
分页存储过程的参数:
--根据条件显示相应的记录,并分页。
ALTER PROC SelectPageFiles
@WhereCondition nvarchar(500), --查询条件
@PageIndex int, --页号
@PageCount int, --每页的记录数
@OrderCondition varchar(50) --排序条件
as.....
调用控件声明:
<%@ register tagprefix="MyTag" tagname="MyDataList" src="~/DataShow.ascx" %>
定义控件:
<MyTag:MyDataList id="MyDataList1" runat="server"></MyTag:MyDataList>
调用分页控件:
this.MyDataList1.BindDataSource(WhereCondition, this.keyword.Text.Trim());