All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
mssql存储过程类型详解_mssql学习_编程技术
C#教程:DNS类使用实例
从HTML到XML_[XML教程]
数据仓库的概念_mssql学习_编程技术
SQL语言快速入门之一_mssql学习_编程技术
.NET数据库应用程序中存储过程的应用_[Asp.Net教程]
delphi存储过程的使用实例
ASP.NET国际化(多语言)支持组件[提供下载]_[Asp.Net教程]
c#的DateTime.Now函数详解_[Asp.Net教程]
SQL Server定时作业的设置方法_[SQL Server教程]
Reading number is top 10 pictures
Household design classic black and white
什么叫国家
2012 national geographic daily picture4
Absolutely shocked. National geographic 50 animal photographys5
The money of more than 100 countries and regions8
Steal to eat bacon bird
China's ambassador to Libya embassy was shock, and the glass is broken in
有种屌丝级别的好妹子
徐若瑄展示美丽胸围3
Fierce! China's special forces training the devil2
Download software ranking
dreamweaver8中文版
Sora aoi 120 minutes
VC++6.0简体中文版
美女写真2
传奇私服架设教程
艳兽都市
Unix video tutorial2
终极变速大师Speeder3.26
Detective task-the top secret prostitution files
Dance with duck(male prostitution)
delv published in(发表于) 2014/1/16 9:33:29 Edit(编辑)
扩展GridView控件(十)——再增加一种分页样式_[Asp.Net教程]

扩展GridView控件(十)——再增加一种分页样式_[Asp.Net教程]

扩展GridView控件(十)——再增加一种分页样式_[Asp.Net教程]

GridView既强大又好用。为了让它更强大、更好用,我们来写一个继承自GridView的控件。
[源码下载]
http://files.cnblogs.com/webabcd/yycontrols.rar


介绍
用着GridView自带的分页样式总觉得不太习惯,我们可以在PagerTemplate中来写一些自定义的样式,但是也挺麻烦的,其实我们可以扩展一下GridView,给它再增加一种分页样式


控件开发
1、新建一个继承自GridView的类。
复制C#代码保存代码///


/// 继承自GridView
///

[ToolboxData(@"<{0}:SmartGridView runat='server'>")]
public class SmartGridView : GridView
{
}
2、新建一个Paging类,定义一个分页样式的枚举
复制C#代码保存代码using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;


namespace YYControls.SmartGridView
{
///


/// 自定义分页相关
///

public class Paging
{
///
/// 自定义分页样式
///

public enum PagingStyleCollection
{
///
/// 不用自定义分页样式
///

None,
///
/// 默认自定义分页样式
///

Default
}
}
}
3、在继承自GridView的类中加一个上面定义的枚举属性
复制C#代码保存代码private Paging.PagingStyleCollection _pagingStyle;
///
/// 自定义分页样式
///

[Description("自定义分页样式"), DefaultValue(""), Category("扩展")]
public Paging.PagingStyleCollection PagingStyle
{
get { return _pagingStyle; }
set { _pagingStyle = value; }
}
4、如果GridView使用的是数据源控件的话,计算总记录数
复制C#代码保存代码///
/// OnLoad
///

///
protected override void OnLoad(EventArgs e)
{
// 查找ObjectDataSource
ObjectDataSource ods = Parent.FindControl(this.DataSourceID) as ObjectDataSource;
if (ods != null)
{
ods.Selected += new ObjectDataSourceStatusEventHandler(ods_Selected);
}


base.OnLoad(e);
}


private int? _recordCount = null;
///


/// 计算总记录数
///

///
///
private void ods_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
if (e.ReturnValue is IListSource)
{
_recordCount = ((IListSource) e.ReturnValue).GetList().Count;
}
}
5、重写OnRowCreated以实现自定义分页样式
复制C#代码保存代码///
/// OnRowCreated
///

///
protected override void OnRowCreated(GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Pager
&& PagingStyle == Paging.PagingStyleCollection.Default)
{
LinkButton First = new LinkButton();
LinkButton Prev = new LinkButton();
LinkButton Next = new LinkButton();
LinkButton Last = new LinkButton();


TableCell tc = new TableCell();


e.Row.Controls.Clear();


tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
if (_recordCount.HasValue)
{
tc.Controls.Add(new LiteralControl(_recordCount.ToString()));
tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
tc.Controls.Add(new LiteralControl(PageSize.ToString()));
tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
}
tc.Controls.Add(new LiteralControl((PageIndex + 1).ToString()));
tc.Controls.Add(new LiteralControl("/"));
tc.Controls.Add(new LiteralControl(PageCount.ToString()));
tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;&nbsp;&nbsp;"));


if (!String.IsNullOrEmpty(PagerSettings.FirstPageImageUrl))
{
First.Text = "";
}
else
{
First.Text = PagerSettings.FirstPageText;
}
First.CommandName = "Page";
First.CommandArgument = "First";
First.Font.Underline = false;


if (!String.IsNullOrEmpty(PagerSettings.PreviousPageImageUrl))
{
Prev.Text = "";
}
else
{
Prev.Text = PagerSettings.PreviousPageText;
}
Prev.CommandName = "Page";
Prev.CommandArgument = "Prev";
Prev.Font.Underline = false;



if (!String.IsNullOrEmpty(PagerSettings.NextPageImageUrl))
{
Next.Text = "";
}
else
{
Next.Text = PagerSettings.NextPageText;
}
Next.CommandName = "Page";
Next.CommandArgument = "Next";
Next.Font.Underline = false;


if (!String.IsNullOrEmpty(PagerSettings.LastPageImageUrl))
{
Last.Text = "";
}
else
{
Last.Text = PagerSettings.LastPageText;
}
Last.CommandName = "Page";
Last.CommandArgument = "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(new LiteralControl("&nbsp;&nbsp;"));
tc.Controls.Add(Prev);
tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));


// 当前页左边显示的数字分页按钮的数量
int rightCount = (int) (PagerSettings.PageButtonCount / 2);
// 当前页右边显示的数字分页按钮的数量
int leftCount = PagerSettings.PageButtonCount % 2 == 0 ? rightCount - 1 : rightCount;
for (int i = 0; i < PageCount; i++)
{
if (PageCount > PagerSettings.PageButtonCount)
{
if (i < PageIndex - leftCount
&& PageCount - 1 - i > PagerSettings.PageButtonCount - 1)
{
continue;
}
else if (i > PageIndex + rightCount
&& i > PagerSettings.PageButtonCount - 1)
{
continue;
}
}


if (i == PageIndex)
{
tc.Controls.Add(new LiteralControl("" + (i + 1).ToString() + ""));
}
else
{
LinkButton lb = new LinkButton();
lb.Text = (i + 1).ToString();
lb.CommandName = "Page";
lb.CommandArgument = (i + 1).ToString();


tc.Controls.Add(lb);
}


tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
}


if (this.PageIndex >= PageCount - 1)
{
Next.Enabled = Last.Enabled = false;
}
else
{
Next.Enabled = Last.Enabled = true;
}
tc.Controls.Add(Next);
tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));
tc.Controls.Add(Last);
tc.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));


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
复制C#代码保存代码using System;
using System.Data;
using System.Configuration;
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 System.ComponentModel;


///


/// OjbData 的摘要说明
///

public class OjbData
{
public OjbData()
{
//
// TOD 在此处添加构造函数逻辑
//
}


[DataObjectMethod(DataObjectMethodType.Select, true)]
public DataTable Select()
{
DataTable dt = new DataTable();
dt.Columns.Add("no", typeof(string));
dt.Columns.Add("name", typeof(string));


for (int i = 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);
}


return dt;
}
}
Default.aspx
复制ASPX代码保存代码<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>





SmartGridView测试





AutoGenerateColumns="False" AllowPaging="true" PagingStyle="Default">









TypeName="OjbData">






转自【webabcd-.NET】







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.