自定义Web DataGrid 翻页控件
2006-03-07 14:40
447 查看
下面就是有关DataGrid的源码,本人在此就不在说废话了,有兴趣的朋友可以Download,一看便知。。。
using System;
using System.ComponentModel;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebControlLib.DataGrid
{
[DefaultProperty("Text"),
ToolboxData("<{0}:DatagridPager runat=server></{0}:DatagridPager>")]
public class DatagridPager : System.Web.UI.WebControls.DataGrid
{
//Static constants
private static readonly Regex RX = new Regex(@"^&page=/d+", RegexOptions.Compiled);
private const string LINK_FIRST = "<a href=?page={0}>[首页]</a>";
private const string LINK_FIRST_DISABLE = "[首页]";
private const string LINK_PREV = "<a href=?page={0}>[上页]</a>";
private const string LINK_PREV_DISABLE = "[上页]";
private const string LINK_NEXT = "<a href=?page={0}>[下页]</a>";
private const string LINK_NEXT_DISABLE = "[下页]";
private const string LINK_LAST = "<a href=?page={0}>[尾页]</a>";
private const string LINK_LAST_DISABLE = "[尾页]";
private const string LINK_PAGES = " 第{0}页/共计{1}页 共{2}笔资料";
//private const string GO_PAGES = " <input type=submit name=action value=到><input type=text name=go_pages size=3>页";
private const string GO_PAGES = " 到:<input type=text name=go_pages size=3 onblur='toPages(this)'>页";
private const string TO_SCRIPT = @"<script language='javascript'>"+
" var pageCount=3;"+
" var to_page=0;"+
" function toPages(e){"+
" if(isInteger(e.value)){"+
" to_page=parseInt(e.value)>parseInt(pageCount)?pageCount:e.value;"+
" var path = location.href+'?';"+
" path = path.substring(0,path.indexOf('?'));"+
" window.location.href=path+'?page='+to_page;document.getElementsByName('go_pages').value=to_page;"+
" }"+
" else"+
" e.value=0;"+
" }"+
" function isInteger(inputVal){"+
" inputStr = inputVal.toString();"+
" for (var i=0; i< inputStr.length; i++){"+
" var oneChar = inputStr.charAt(i);"+
" if (oneChar < '0' || oneChar > '9')"+
" return false;"+
" else"+
" return true;"+
" }"+
" }"+
"</script>";
private const string ScriptKey="HcTextBoxClientScrptKeyWord";
private const string KEY_PAGE = "page";
private const string KEY_GO_PAGE = "go_pages";
private const string COMMA = "?";
private const string AMP = "&";
protected string emptyText;
protected string htmlHiddenSortExpression;
//private int pageingStyle = 0; //0:表示内定换页方式,1:表示自定义的方式,就是本类的方式。
private IList dataSource;
private int pageSize = 10;
private int currentPageIndex;
private int itemCount;
override public object DataSource
{
set
{
//This try catch block is to avoid issues with the VS.NET designer
//The designer will try and bind a datasource which does not derive from ILIST
try
{
dataSource = (IList)value;
ItemCount = dataSource.Count;
}
catch
{
dataSource = null;
ItemCount = 0;
}
}
}
public override int PageSize
{
get { return pageSize; }
set { pageSize = value; }
}
new protected int PageCount
{
get { return ((ItemCount - 1) / pageSize)+1; }
}
virtual protected int ItemCount
{
get { return itemCount; }
set { itemCount = value; }
}
new virtual public int CurrentPageIndex
{
get { return currentPageIndex; }
set { currentPageIndex = value; }
}
public string EmptyText
{
set { emptyText = value; }
}
public string HiddenSortExpression
{
get { return htmlHiddenSortExpression; }
set { htmlHiddenSortExpression = value; }
}
public void SetPage(int index)
{
OnPageIndexChanged(new DataGridPageChangedEventArgs(null, index));
}
override protected void OnLoad(EventArgs e)
{
if (Visible)
{
string sorts = Context.Request["sort"];
htmlHiddenSortExpression = (sorts != string.Empty) ? sorts : string.Empty;
int iGoPages = 0;
int pages = 1;
string go_page = Context.Request[KEY_GO_PAGE];
string page = Context.Request[KEY_PAGE];
try
{
iGoPages = (go_page != null) ? int.Parse(go_page) : 0;
pages = (page != null) ? int.Parse(page) : 1;
}
catch(Exception)
{
return;
}
if (iGoPages > 0 && iGoPages <= PageCount)
{
SetPage(iGoPages - 1);
}
else
{
SetPage(pages - 1);
}
}
}
/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="output"> 要写出到的 HTML 编写器 </param>
protected override void OnPreRender(System.EventArgs e)
{
Page.RegisterStartupScript(ScriptKey,TO_SCRIPT);
}
/// <summary>
/// Overriden method to control how the page is rendered
/// </summary>
/// <param name="writer"></param>
override protected void Render(HtmlTextWriter writer)
{
// Check there is some data attached
if (ItemCount == 0)
{
writer.Write(emptyText);
return;
}
if (AllowPaging == false || PagerStyle.Visible == true)
{
base.Render(writer);
return;
}
//Mask the query
string query = Context.Request.Url.Query.Replace(COMMA, AMP);
query = RX.Replace(query, string.Empty);
if (htmlHiddenSortExpression != string.Empty)
{
if (query.IndexOf("&sort=") >= 0)
{
query = query.Substring(0, query.IndexOf("&sort="));
}
query = query + "&sort=" + htmlHiddenSortExpression;
}
// Call the inherited method
base.Render(writer);
//Determin whether next and previous buttons are required
//Previous button?
int currentPage = CurrentPageIndex + 1;
if (currentPage < 1)
{
currentPage = 1;
}
if (currentPage == 1)
{
writer.Write(LINK_FIRST_DISABLE);
writer.Write(LINK_PREV_DISABLE);
if (PageCount > 1)
{
writer.Write(string.Format(LINK_NEXT, (currentPage + 1) + query));
writer.Write(string.Format(LINK_LAST, (PageCount) + query));
}
else
{
writer.Write(LINK_NEXT_DISABLE);
writer.Write(LINK_LAST_DISABLE);
}
}
else
{
writer.Write(string.Format(LINK_FIRST, (1) + query));
writer.Write(string.Format(LINK_PREV, (currentPage - 1) + query));
if (currentPage == PageCount)
{
writer.Write(LINK_NEXT_DISABLE);
writer.Write(LINK_LAST_DISABLE);
}
else
{
writer.Write(string.Format(LINK_NEXT, (currentPage + 1) + query));
writer.Write(string.Format(LINK_LAST, (PageCount) + query));
}
}
writer.Write(string.Format(LINK_PAGES, currentPage, PageCount,ItemCount));
writer.Write(string.Format(GO_PAGES));
}
override protected void OnDataBinding(EventArgs e)
{
//Work out which items we want to render to the page
int start = CurrentPageIndex * pageSize;
int size = Math.Min(pageSize, ItemCount - start);
IList page = new ArrayList();
//Add the relevant items from the datasource
for (int i = 0; i < size; i++)
page.Add(dataSource[start + i]);
//set the base objects datasource
base.DataSource = page;
base.OnDataBinding(e);
}
new public event DataGridPageChangedEventHandler PageIndexChanged;
new virtual protected void OnPageIndexChanged(DataGridPageChangedEventArgs e)
{
if (PageIndexChanged != null)
PageIndexChanged(this, e);
}
// protected override void AddAttributesToRender(HtmlTextWriter output)
// {
// output.AddAttribute("onfocus","setday(this);");
// base.AddAttributesToRender(output);
// }
}
}
using System;
using System.ComponentModel;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebControlLib.DataGrid
{
[DefaultProperty("Text"),
ToolboxData("<{0}:DatagridPager runat=server></{0}:DatagridPager>")]
public class DatagridPager : System.Web.UI.WebControls.DataGrid
{
//Static constants
private static readonly Regex RX = new Regex(@"^&page=/d+", RegexOptions.Compiled);
private const string LINK_FIRST = "<a href=?page={0}>[首页]</a>";
private const string LINK_FIRST_DISABLE = "[首页]";
private const string LINK_PREV = "<a href=?page={0}>[上页]</a>";
private const string LINK_PREV_DISABLE = "[上页]";
private const string LINK_NEXT = "<a href=?page={0}>[下页]</a>";
private const string LINK_NEXT_DISABLE = "[下页]";
private const string LINK_LAST = "<a href=?page={0}>[尾页]</a>";
private const string LINK_LAST_DISABLE = "[尾页]";
private const string LINK_PAGES = " 第{0}页/共计{1}页 共{2}笔资料";
//private const string GO_PAGES = " <input type=submit name=action value=到><input type=text name=go_pages size=3>页";
private const string GO_PAGES = " 到:<input type=text name=go_pages size=3 onblur='toPages(this)'>页";
private const string TO_SCRIPT = @"<script language='javascript'>"+
" var pageCount=3;"+
" var to_page=0;"+
" function toPages(e){"+
" if(isInteger(e.value)){"+
" to_page=parseInt(e.value)>parseInt(pageCount)?pageCount:e.value;"+
" var path = location.href+'?';"+
" path = path.substring(0,path.indexOf('?'));"+
" window.location.href=path+'?page='+to_page;document.getElementsByName('go_pages').value=to_page;"+
" }"+
" else"+
" e.value=0;"+
" }"+
" function isInteger(inputVal){"+
" inputStr = inputVal.toString();"+
" for (var i=0; i< inputStr.length; i++){"+
" var oneChar = inputStr.charAt(i);"+
" if (oneChar < '0' || oneChar > '9')"+
" return false;"+
" else"+
" return true;"+
" }"+
" }"+
"</script>";
private const string ScriptKey="HcTextBoxClientScrptKeyWord";
private const string KEY_PAGE = "page";
private const string KEY_GO_PAGE = "go_pages";
private const string COMMA = "?";
private const string AMP = "&";
protected string emptyText;
protected string htmlHiddenSortExpression;
//private int pageingStyle = 0; //0:表示内定换页方式,1:表示自定义的方式,就是本类的方式。
private IList dataSource;
private int pageSize = 10;
private int currentPageIndex;
private int itemCount;
override public object DataSource
{
set
{
//This try catch block is to avoid issues with the VS.NET designer
//The designer will try and bind a datasource which does not derive from ILIST
try
{
dataSource = (IList)value;
ItemCount = dataSource.Count;
}
catch
{
dataSource = null;
ItemCount = 0;
}
}
}
public override int PageSize
{
get { return pageSize; }
set { pageSize = value; }
}
new protected int PageCount
{
get { return ((ItemCount - 1) / pageSize)+1; }
}
virtual protected int ItemCount
{
get { return itemCount; }
set { itemCount = value; }
}
new virtual public int CurrentPageIndex
{
get { return currentPageIndex; }
set { currentPageIndex = value; }
}
public string EmptyText
{
set { emptyText = value; }
}
public string HiddenSortExpression
{
get { return htmlHiddenSortExpression; }
set { htmlHiddenSortExpression = value; }
}
public void SetPage(int index)
{
OnPageIndexChanged(new DataGridPageChangedEventArgs(null, index));
}
override protected void OnLoad(EventArgs e)
{
if (Visible)
{
string sorts = Context.Request["sort"];
htmlHiddenSortExpression = (sorts != string.Empty) ? sorts : string.Empty;
int iGoPages = 0;
int pages = 1;
string go_page = Context.Request[KEY_GO_PAGE];
string page = Context.Request[KEY_PAGE];
try
{
iGoPages = (go_page != null) ? int.Parse(go_page) : 0;
pages = (page != null) ? int.Parse(page) : 1;
}
catch(Exception)
{
return;
}
if (iGoPages > 0 && iGoPages <= PageCount)
{
SetPage(iGoPages - 1);
}
else
{
SetPage(pages - 1);
}
}
}
/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="output"> 要写出到的 HTML 编写器 </param>
protected override void OnPreRender(System.EventArgs e)
{
Page.RegisterStartupScript(ScriptKey,TO_SCRIPT);
}
/// <summary>
/// Overriden method to control how the page is rendered
/// </summary>
/// <param name="writer"></param>
override protected void Render(HtmlTextWriter writer)
{
// Check there is some data attached
if (ItemCount == 0)
{
writer.Write(emptyText);
return;
}
if (AllowPaging == false || PagerStyle.Visible == true)
{
base.Render(writer);
return;
}
//Mask the query
string query = Context.Request.Url.Query.Replace(COMMA, AMP);
query = RX.Replace(query, string.Empty);
if (htmlHiddenSortExpression != string.Empty)
{
if (query.IndexOf("&sort=") >= 0)
{
query = query.Substring(0, query.IndexOf("&sort="));
}
query = query + "&sort=" + htmlHiddenSortExpression;
}
// Call the inherited method
base.Render(writer);
//Determin whether next and previous buttons are required
//Previous button?
int currentPage = CurrentPageIndex + 1;
if (currentPage < 1)
{
currentPage = 1;
}
if (currentPage == 1)
{
writer.Write(LINK_FIRST_DISABLE);
writer.Write(LINK_PREV_DISABLE);
if (PageCount > 1)
{
writer.Write(string.Format(LINK_NEXT, (currentPage + 1) + query));
writer.Write(string.Format(LINK_LAST, (PageCount) + query));
}
else
{
writer.Write(LINK_NEXT_DISABLE);
writer.Write(LINK_LAST_DISABLE);
}
}
else
{
writer.Write(string.Format(LINK_FIRST, (1) + query));
writer.Write(string.Format(LINK_PREV, (currentPage - 1) + query));
if (currentPage == PageCount)
{
writer.Write(LINK_NEXT_DISABLE);
writer.Write(LINK_LAST_DISABLE);
}
else
{
writer.Write(string.Format(LINK_NEXT, (currentPage + 1) + query));
writer.Write(string.Format(LINK_LAST, (PageCount) + query));
}
}
writer.Write(string.Format(LINK_PAGES, currentPage, PageCount,ItemCount));
writer.Write(string.Format(GO_PAGES));
}
override protected void OnDataBinding(EventArgs e)
{
//Work out which items we want to render to the page
int start = CurrentPageIndex * pageSize;
int size = Math.Min(pageSize, ItemCount - start);
IList page = new ArrayList();
//Add the relevant items from the datasource
for (int i = 0; i < size; i++)
page.Add(dataSource[start + i]);
//set the base objects datasource
base.DataSource = page;
base.OnDataBinding(e);
}
new public event DataGridPageChangedEventHandler PageIndexChanged;
new virtual protected void OnPageIndexChanged(DataGridPageChangedEventArgs e)
{
if (PageIndexChanged != null)
PageIndexChanged(this, e);
}
// protected override void AddAttributesToRender(HtmlTextWriter output)
// {
// output.AddAttribute("onfocus","setday(this);");
// base.AddAttributesToRender(output);
// }
}
}
相关文章推荐
- DataGrid自定义分页控件WebControl
- 自定义DataGrid翻页控件(示例代码下载)
- Control Study -> 自定义DataGrid翻页控件(示例代码下载)
- Control Study -> 自定义DataGrid翻页控件(示例代码下载)
- Control Study -> 自定义DataGrid翻页控件(示例代码下载)
- 给web用户控件自定义后台事件
- 背水一战 Windows 10 (64) - 控件(WebView): 加载指定 HttpMethod 的请求, 自定义请求的 http header, app 与 js 的交互
- 利用 ASP.NET 2.0 创建自定义 Web 控件
- 利用ASP.NET 2.0创建自定义Web控件
- 利用ASP.NET 2.0创建自定义Web控件
- 自定义web控件引用外部资源
- DataGrid Web控件深度历险(2) Part1
- Web自定义表单集成FusionCharts图形控件
- 为DataGrid创建自定义列控件
- 关于DataGrid Web控件的热门问题
- asp.net web页面自定义分页控件
- 为DataGrid创建自定义列控件(二)
- NetAdvantage webdatagrid 控件的一些属性
- Control Study->AdRotator广告控件显示自定义配置文件中广告以及与DataGrid控件结合使用显示图片(示例代码下载)
- 解决Asp.net中翻页问题的自定义用户控件