5、重写OnRowCreated以实现自定义分页样式
/// <summary>
///OnRowCreated
/// </summary>
/// <param name="e"></param>
protectedoverridevoidOnRowCreated(GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.Pager && PagingStyle == Paging.PagingStyleCollection.Default)
{
LinkButton First =newLinkButton();
LinkButton Prev =newLinkButton();
LinkButton Next =newLinkButton();
LinkButton Last =newLinkButton();
TableCell tc =newTableCell();
e.Row.Controls.Clear();
tc.Controls.Add(newLiteralControl(""));
if(_recordCount.HasValue)
{
tc.Controls.Add(newLiteralControl(_recordCount.ToString()));
tc.Controls.Add(newLiteralControl(""));
tc.Controls.Add(newLiteralControl(PageSize.ToString()));
tc.Controls.Add(newLiteralControl(""));
}
tc.Controls.Add(newLiteralControl((PageIndex + 1).ToString()));
tc.Controls.Add(newLiteralControl("/"));
tc.Controls.Add(newLiteralControl(PageCount.ToString()));
tc.Controls.Add(newLiteralControl(""));
if(!String.IsNullOrEmpty(PagerSettings.FirstPageImageUrl))
{
First.Text ="<img src='"+ ResolveUrl(PagerSettings.FirstPageImageUrl) +"' border='0'/>";
}
else
{
First.Text = PagerSettings.FirstPageText;
}
mandName ="Page";
mandArgument ="First";
First.Font.Underline =false;
if(!String.IsNullOrEmpty(PagerSettings.PreviousPageImageUrl))
{
Prev.Text ="<img src='"+ ResolveUrl(PagerSettings.PreviousPageImageUrl) +"' border='0'/>";
}
else
{
Prev.Text = PagerSettings.PreviousPageText;
}
mandName ="Page";
mandArgument ="Prev";
Prev.Font.Underline =false;
if(!String.IsNullOrEmpty(PagerSettings.NextPageImageUrl))
{
Next.Text ="<img src='"+ ResolveUrl(PagerSettings.NextPageImageUrl) +"' border='0'/>";
}
else
{
Next.Text = PagerSettings.NextPageText;
}
mandName ="Page";
mandArgument ="Next";
Next.Font.Underline =false;
if(!String.IsNullOrEmpty(PagerSettings.LastPageImageUrl))
{
Last.Text ="<img src='"+ ResolveUrl(PagerSettings.LastPageImageUrl) +"' border='0'/>";
}
else
{
Last.Text = PagerSettings.LastPageText;
}
mandName ="Page";
mandArgument ="Last";
Last.Font.Underline =false;
if(this.PageIndex <= 0)
{
First.Enabled = Prev.Enabled =false;
}
else
{
First.Enabled = Prev.Enabled =true;
}
tc.Controls.Add(First);
tc.Controls.Add(newLiteralControl(""));
tc.Controls.Add(Prev);
tc.Controls.Add(newLiteralControl(""));
// 当前页左边显示的数字分页按钮的数量
intrightCount = (int)(PagerSettings.PageButtonCount / 2);
// 当前页右边显示的数字分页按钮的数量
intleftCount = PagerSettings.PageButtonCount % 2 == 0 ? rightCount - 1 : rightCount;
for(inti = 0; i < PageCount; i++)
{
if(PageCount > PagerSettings.PageButtonCount)
{
if(i < PageIndex - leftCount && PageCount - 1 - i > PagerSettings.PageButtonCount - 1)
{
continue;
}
elseif(i > PageIndex + rightCount && i > PagerSettings.PageButtonCount - 1)
{
continue;
}
}
if(i == PageIndex)
{
tc.Controls.Add(newLiteralControl("<span style='color:red;font-weight:bold'>"+ (i + 1).ToString() +"</span>"));
}
else
{
LinkButton lb =newLinkButton();
lb.Text = (i + 1).ToString();
mandName ="Page";
mandArgument = (i + 1).ToString();
tc.Controls.Add(lb);
}
tc.Controls.Add(newLiteralControl(""));
}
if(this.PageIndex >= PageCount - 1)
{
Next.Enabled = Last.Enabled =false;
}
else
{
Next.Enabled = Last.Enabled =true;
}
tc.Controls.Add(Next);
tc.Controls.Add(newLiteralControl(""));
tc.Controls.Add(Last);
tc.Controls.Add(newLiteralControl(""));
tc.ColumnSpan =this.Columns.Count;
e.Row.Controls.Add(tc);
}
base.OnRowCreated(e);
} 控件使用
添加这个控件到工具箱里,然后拖拽到webform上,设置PagingStyle属性为Default,同时设置GridView的原有属性PageButtonCount,FirstPageText,PreviousPageText,NextPageText,LastPageText,FirstPageImageUrl,PreviousPageImageUrl,NextPageImageUrl,LastPageImageUrl
ObjData.cs usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
ponentModel;
/// <summary>
///OjbData 的摘要说明
/// </summary>
publicclassOjbData
{
publicOjbData()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
[DataObjectMethod(DataObjectMethodType.Select,true)]
publicDataTable Select()
{
DataTable dt =newDataTable();
dt.Columns.Add("no",typeof(string));
dt.Columns.Add("name",typeof(string));
for(inti = 0; i < 30; i++)
{
DataRow dr = dt.NewRow();
dr[0] ="no"+ i.ToString().PadLeft(2, '0');
dr[1] ="name"+ i.ToString().PadLeft(2, '0');
dt.Rows.Add(dr);
}
returndt;
}
} Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="/1999/xhtml">
<head runat="server">
<title>SmartGridView测试</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<yyc:SmartGridView ID="SmartGridView1" runat="server" DataSourceID="ObjectDataSource1"
AutoGenerateColumns="False" AllowPaging="true" PagingStyle="Default">
<Columns>
<asp:BoundField DataField="no" HeaderText="序号" SortExpression="no" ItemStyle-Width="100px" />
<asp:BoundField DataField="name" HeaderText="名称" SortExpression="name" ItemStyle-Width="100px" />
<asp:BoundField DataField="no" HeaderText="序号" SortExpression="no" ItemStyle-Width="100px" />
<asp:BoundField DataField="name" HeaderText="名称" SortExpression="name" ItemStyle-Width="100px" />
<asp:BoundField DataField="no" HeaderText="序号" SortExpression="no" ItemStyle-Width="100px" />
<asp:BoundField DataField="name" HeaderText="名称" SortExpression="name" ItemStyle-Width="100px" />
</Columns>
</yyc:SmartGridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Select"
TypeName="OjbData"></asp:ObjectDataSource>
</div>
</form>
</body>
</html> /*测试版的实现 结束*/
OK
[源码下载]
本文转自webabcd 51CTO博客,原文链接:/webabcd/345576,如需转载请自行联系原作者
如果觉得《上接扩展GridView控件(10) - 自定义分页样式》对你有帮助,请点赞、收藏,并留下你的观点哦!