GridView 简单扩展
生活随笔
收集整理的這篇文章主要介紹了
GridView 简单扩展
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
using?System;using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Text;
using?System.Web;
using?System.Web.UI;
using?System.Web.UI.WebControls;
namespace?Fanfajin.MyWebControls
{
????/**////?<summary>
????///?增強的?GridView?控件?擴展?
????///?</summary>
????[DefaultProperty("SelectedValue")]
????[ToolboxData("<{0}:MyGridView?runat=server></{0}:MyGridView>")]
????public?sealed?class?MyGridView?:?System.Web.UI.WebControls.GridView
????{
????????公開屬性成員#region?公開屬性成員
????????/**////?<summary>
????????///?鼠標經過時顏色
????????///?</summary>??????
????????[Category("擴展設置")]
????????[Description("鼠標經過時顏色")]
????????public?string?MouseOverColor
????????{
????????????get
????????????{
????????????????if?(ViewState["MouseOverColor"]?==?null)
????????????????{
????????????????????ViewState["MouseOverColor"]?=?"#9900FF";
????????????????}
????????????????return?(string)ViewState["MouseOverColor"];
????????????}
????????????set
????????????{
????????????????ViewState["MouseOverColor"]?=?value;
????????????}
????????}
????????/**////?<summary>
????????///?鼠標經過時顏色
????????///?</summary>??????
????????[Category("擴展設置")]
????????[Description("鼠標離開時顏色")]
????????public?string?MouseOutColor
????????{
????????????get
????????????{
????????????????if?(ViewState["MouseOutColor"]?==?null)
????????????????{
????????????????????ViewState["MouseOutColor"]?=?"#FFFFFF";
????????????????}
????????????????return?(string)ViewState["MouseOutColor"];
????????????}
????????????set
????????????{
????????????????ViewState["MouseOutColor"]?=?value;
????????????}
????????}
????????/**////?<summary>
????????///?是否啟用?HighlightColor
????????///?</summary>
????????[Category("擴展設置")]
????????[Description("是否啟用?MouseOverColor")]
????????public?bool?IsOpenMouseOverColor
????????{
????????????get
????????????{
????????????????if?(ViewState["enableSelection"]?==?null)
????????????????{
????????????????????ViewState["enableSelection"]?=?true;
????????????????}
????????????????return?(bool)ViewState["enableSelection"];
????????????}
????????????set
????????????{
????????????????ViewState["enableSelection"]?=?value;
????????????}
????????}
????????/**////?<summary>
????????///?是否啟用擴展
????????///?</summary>
????????[Category("擴展設置")]
????????[Description("是否啟用擴展")]
????????public?bool?ActivePagerBar
????????{
????????????get
????????????{
????????????????if?(ViewState["enableLNSeaPager"]?==?null)
????????????????{
????????????????????ViewState["enableLNSeaPager"]?=?true;
????????????????}
????????????????return?(bool)ViewState["enableLNSeaPager"];
????????????}
????????????set
????????????{
????????????????this.AllowPaging?=?value;
????????????????ViewState["enableLNSeaPager"]?=?value;
????????????}
????????}
????????/**////?<summary>
????????///?默認排序(圖標顯示升序降序時用)
????????///?</summary>
????????[Category("擴展設置")]
????????[Description("默認排序(圖標顯示升序降序時用)")]
????????public?SortDirection?SortDirectionAlt
????????{
????????????get
????????????{
????????????????if?(ViewState["sortDirection"]?==?null)
????????????????{
????????????????????ViewState["sortDirection"]?=?SortDirection.Ascending;
????????????????}
????????????????return?(SortDirection)ViewState["sortDirection"];
????????????}
????????????set
????????????{
????????????????ViewState["sortDirection"]?=?value;
????????????}
????????}
????????/**////?<summary>
????????///?默認排序字段(圖標顯示升序降序時用)
????????///?</summary>
????????[Category("擴展設置")]
????????[Description("默認排序字段(圖標顯示升序降序時用)")]
????????public?string?SortExpressionAlt
????????{
????????????get
????????????{
????????????????if?(ViewState["sortExpressionAlt"]?==?null)
????????????????{
????????????????????ViewState["sortExpressionAlt"]?=?"";
????????????????}
????????????????return?(string)ViewState["sortExpressionAlt"];
????????????}
????????????set
????????????{
????????????????ViewState["sortExpressionAlt"]?=?value;
????????????}
????????}
????????/**////?<summary>
????????///?Get?or?Set?Image?location?to?be?used?to?display?Ascending?Sort?order.
????????///?</summary>
????????[
????????Description("降序時顯示的圖片URL"),
????????Category("擴展設置"),
????????Editor("System.Web.UI.Design.UrlEditor",?typeof(System.Drawing.Design.UITypeEditor)),
????????DefaultValue(""),
????????]
????????public?string?SortDescImageUrl
????????{
????????????get
????????????{
????????????????object?o?=?ViewState["SortImageDesc"];
????????????????return?(o?!=?null???o.ToString()?:?"");
????????????}
????????????set
????????????{
????????????????ViewState["SortImageDesc"]?=?value;
????????????}
????????}
????????/**////?<summary>
????????///?Get?or?Set?Image?location?to?be?used?to?display?Ascending?Sort?order.
????????///?</summary>
????????[
????????Description("升序時顯示的圖片"),
????????Category("擴展設置"),
????????Editor("System.Web.UI.Design.UrlEditor",?typeof(System.Drawing.Design.UITypeEditor)),
????????DefaultValue(""),
????????]
????????public?string?SortAscImageUrl
????????{
????????????get
????????????{
????????????????object?o?=?ViewState["SortImageAsc"];
????????????????return?(o?!=?null???o.ToString()?:?"");
????????????}
????????????set
????????????{
????????????????ViewState["SortImageAsc"]?=?value;
????????????}
????????}
????????/**////?<summary>
????????///?是否顯示升序降序圖標
????????///?</summary>
????????[
????????Description("是否顯示升序降序圖片URL"),
????????Category("擴展設置"),
????????Editor("System.Web.UI.Design.UrlEditor",?typeof(System.Drawing.Design.UITypeEditor)),
????????DefaultValue("false"),
????????]
????????public?bool?IsShowSortDirectionImg
????????{
????????????get
????????????{
????????????????object?o?=?ViewState["ShowSortDirection"];
????????????????return?(o?!=?null???Convert.ToBoolean(o)?:?false);
????????????}
????????????set
????????????{
????????????????ViewState["ShowSortDirection"]?=?value;
????????????}
????????}
????????/**////?<summary>
????????///?分頁信息當前第{0}頁共{1}頁{2}條記錄
????????///?</summary>
????????[Category("擴展設置")]
????????[Description("分頁信息當前第{0}頁共{1}頁{2}條記錄")]
????????public?string?PageInfo
????????{
????????????get
????????????{
????????????????if?(ViewState["PageInfo"]?==?null)
????????????????{
????????????????????ViewState["PageInfo"]?=?"當前{0},共{1}頁{2}條記錄";
????????????????}
????????????????return?(string)ViewState["PageInfo"];
????????????}
????????????set
????????????{
????????????????ViewState["PageInfo"]?=?value;
????????????}
????????}
????????/**////?<summary>
????????///?當前頁左右的數字個數(不包括智能的)
????????///?</summary>
????????[Category("擴展設置")]
????????[Description("當前頁左右的數字個數(不包括智能的)")]
????????public?int?NumCount
????????{
????????????get
????????????{
????????????????if?(ViewState["NumCount"]?==?null)
????????????????{
????????????????????ViewState["NumCount"]?=?5;
????????????????}
????????????????return?(int)ViewState["NumCount"];
????????????}
????????????set
????????????{
????????????????ViewState["NumCount"]?=?value;
????????????}
????????}
????????/**////?<summary>
????????///?是否顯示智能數字
????????///?</summary>
????????[Category("擴展設置")]
????????[Description("是否顯示智能數字")]
????????public?bool?IsShowSmartPage
????????{
????????????get
????????????{
????????????????if?(ViewState["IsShowSmartPage"]?==?null)
????????????????{
????????????????????ViewState["IsShowSmartPage"]?=?true;
????????????????}
????????????????return?(bool)ViewState["IsShowSmartPage"];
????????????}
????????????set
????????????{
????????????????ViewState["IsShowSmartPage"]?=?value;
????????????}
????????}
????????/**////?<summary>
????????///?分頁信息顯示在左邊還是在右邊
????????///?</summary>
????????[Category("擴展設置")]
????????[Description("是否啟用?HighlightColor")]
????????public?bool?PageInfoShowLeft
????????{
????????????get
????????????{
????????????????if?(ViewState["PageInfoShowLeft"]?==?null)
????????????????{
????????????????????ViewState["PageInfoShowLeft"]?=?true;
????????????????}
????????????????return?(bool)ViewState["PageInfoShowLeft"];
????????????}
????????????set
????????????{
????????????????ViewState["PageInfoShowLeft"]?=?value;
????????????}
????????}
????????/**////?<summary>
????????///?智能導航數字的第一個的倍數
????????///?</summary>
????????[Category("擴展設置")]
????????[Description("智能導航數字的第一個的倍數")]
????????public?int?BeiShu
????????{
????????????get
????????????{
????????????????if?(ViewState["BeiShu"]?==?null)
????????????????{
????????????????????ViewState["BeiShu"]?=?10;
????????????????}
????????????????return?(int)ViewState["BeiShu"];
????????????}
????????????set
????????????{
????????????????ViewState["BeiShu"]?=?value;
????????????}
????????}
????????/**////?<summary>
????????///?當前頁的顏色
????????///?</summary>??????
????????[Category("擴展設置")]
????????[Description("當前頁的顏色")]
????????public?string?CurrentPageColor
????????{
????????????get
????????????{
????????????????if?(ViewState["CurrentPageColor"]?==?null)
????????????????{
????????????????????ViewState["CurrentPageColor"]?=?"red";
????????????????}
????????????????return?(string)ViewState["CurrentPageColor"];
????????????}
????????????set
????????????{
????????????????ViewState["CurrentPageColor"]?=?value;
????????????}
????????}
????????/**////?<summary>
????????///?智能數字的顏色
????????///?</summary>??????
????????[Category("擴展設置")]
????????[Description("智能數字的顏色")]
????????public?string?SmartPageColor
????????{
????????????get
????????????{
????????????????if?(ViewState["SmartPageColor"]?==?null)
????????????????{
????????????????????ViewState["SmartPageColor"]?=?"#00FF00";
????????????????}
????????????????return?(string)ViewState["SmartPageColor"];
????????????}
????????????set
????????????{
????????????????ViewState["SmartPageColor"]?=?value;
????????????}
????????}
????????#endregion
????????/**////?<summary>
????????///?When?using?GridView?in?certain?ways?the?SortDirection?and?SortExpression
????????///?properties?are?sometimes?left?blank?or?never?changed.?When?using?this?control,
????????///?the?Alt?properties?remedy?this?situation.
????????///?</summary>
????????///?<param?name="e"></param>
????????protected?override?void?OnSorting(GridViewSortEventArgs?e)
????????{
????????????//Handle?setting?up?of?sorting?info?
????????????if?(!String.IsNullOrEmpty(this.SortExpression))
????????????{
????????????????SortExpressionAlt?=?e.SortExpression;
????????????????SortDirectionAlt?=?e.SortDirection;
????????????}
????????????else
????????????{
????????????????if?(SortExpressionAlt?==?e.SortExpression)
????????????????{
????????????????????if?(SortDirectionAlt?==?SortDirection.Ascending)
????????????????????{
????????????????????????SortDirectionAlt?=?SortDirection.Descending;
????????????????????}
????????????????????else
????????????????????{
????????????????????????SortDirectionAlt?=?SortDirection.Ascending;
????????????????????}
????????????????}
????????????????else
????????????????{
????????????????????SortDirectionAlt?=?SortDirection.Ascending;
????????????????}
????????????????this.SortExpressionAlt?=?e.SortExpression;
????????????}
????????????base.OnSorting(e);
????????}
????????初始化在分頁功能啟用時顯示的頁導航行。#region?初始化在分頁功能啟用時顯示的頁導航行。
????????/**////?<summary>
????????///?初始化在分頁功能啟用時顯示的頁導航行。
????????///?</summary>
????????///?<param?name="row">一個?GridViewRow,表示要初始化的頁導航行。?</param>
????????///?<param?name="columnSpan">頁導航行應跨越的列數</param>
????????///?<param?name="pagedDataSource">一個?PagedDataSource,表示數據源。?</param>
????????protected?override?void?InitializePager(GridViewRow?row,?int?columnSpan,?PagedDataSource?pagedDataSource)
????????{
????????????string?pageinfo?=?this.PageInfo.Replace("{0}",?(pagedDataSource.CurrentPageIndex?+?1).ToString()).Replace
????????????????("{1}",?pagedDataSource.PageCount.ToString()).Replace("{2}",?pagedDataSource.DataSourceCount.ToString());
????????????if?(this.ActivePagerBar)
????????????{
????????????????base.PagerTemplate?=?new?ExtendGridViewPagerTemplate(
????????????????????this.PageIndex,
????????????????????this.PageCount,
????????????????????this.PagerSettings.FirstPageText,
????????????????????this.PagerSettings.PreviousPageText,
????????????????????this.PagerSettings.NextPageText,
????????????????????this.PagerSettings.LastPageText,
????????????????????pageinfo,
????????????????????this.NumCount,
????????????????????this.IsShowSmartPage,
????????????????????this.BeiShu,
????????????????????this.PageInfoShowLeft,
????????????????????this.SmartPageColor,
????????????????????this.CurrentPageColor
????????????????????);
????????????}
????????????base.InitializePager(row,?columnSpan,?pagedDataSource);
????????}
????????#endregion
????????/**////?<summary>
????????///?呈現?GridView?控件之前,必須先為該控件中的每一行創建一個?GridViewRow?對象。
????????///?在創建?GridView?控件中的每一行時,將引發?RowCreated?事件。這使您可以提供一個這樣的事件處理方法,
????????///?即每次發生此事件時都執行一個自定義例程(如在行中添加自定義內容)。
????????///?</summary>
????????///?<param?name="e"></param>
????????protected?override?void?OnRowCreated(GridViewRowEventArgs?e)
????????{
????????????//給繼承者的說明?在派生類中重寫?OnRowCreated?時,一定要調用基類的?OnRowCreated?方法,以便已注冊的委托對事件進行接收。
????????????base.OnRowCreated(e);
????????????if?(e.Row.RowType?==?DataControlRowType.DataRow)
????????????{
????????????????//If?row?selection?is?enabled?then?add?mouse?over?scripts?to?enable?on?client.
????????????????if?(IsOpenMouseOverColor)
????????????????{
????????????????????e.Row.Attributes.Add("onmouseover",?"this.style.backgroundColor?=?'"?+?this.MouseOverColor?+?"';");
????????????????????e.Row.Attributes.Add("onmouseout",?"this.style.backgroundColor?=?'"?+?this.MouseOutColor?+?"';");
????????????????}
????????????}
????????????else?if?(e.Row.RowType?==?DataControlRowType.Header?&&?IsShowSortDirectionImg)
????????????{
????????????????foreach?(TableCell?headerCell?in?e.Row.Cells)
????????????????{
????????????????????if?(headerCell.HasControls())
????????????????????{
????????????????????????AddSortImageToHeaderCell(headerCell);
????????????????????}
????????????????}
????????????}
????????}
????????//Header中加入排序的圖片
????????private?void?AddSortImageToHeaderCell(TableCell?headerCell)
????????{
????????????//?查出headerCell中的?linkButton
????????????LinkButton?lnk?=?(LinkButton)headerCell.Controls[0];
????????????if?(lnk?!=?null)
????????????{
????????????????System.Web.UI.WebControls.Image?img?=?new?System.Web.UI.WebControls.Image();
????????????????//?設置圖片的URL
????????????????img.ImageUrl?=?(this.SortDirectionAlt?==?SortDirection.Ascending???this.SortAscImageUrl?:?this.SortDescImageUrl);
????????????????//?如果用戶選擇了排序,則加入排序圖片
????????????????if?(this.SortExpressionAlt?==?lnk.CommandArgument)
????????????????{
????????????????????//加入一個空格
????????????????????headerCell.Controls.Add(new?LiteralControl("?"));
????????????????????headerCell.Controls.Add(img);
????????????????}
????????????}
????????}
????}
????/**////?<summary>
????///?分頁導航條擴展
????///?</summary>
????class?ExtendGridViewPagerTemplate?:?ITemplate
????{
????????私有成員#region?私有成員
????????int?_pageIndex;//當前頁????
????????int?_pageNumber
????????{
????????????get
????????????{
????????????????return?_pageIndex?+?1;
????????????}
????????????set
????????????{
????????????????_pageIndex?=?value?-?1;
????????????}
????????}//當前頁?第一頁為?1
????????int?_pageCount;//總頁數
????????string?_First;
????????string?_Prev;
????????string?_Next;
????????string?_Last;
????????string?_PageInfo;
????????int?_NumCount;
????????int?_Beishu?=?3;
????????int?_StartPage;
????????int?_EndPage;
????????int?_MoreStartPage1;
????????int?_MoreStartPage2;
????????int?_MoreStartPage3;
????????int?_MoreEndPage1;
????????int?_MoreEndPage2;
????????int?_MoreEndPage3;
????????bool?_IsShowSmartPage?=?true;
????????bool?_PageInfoShowLeft?=?true;
????????string?_CurrentPageColor?=?"red";
????????string?_SmartPageColor?=?"#00FF00";
????????#endregion
????????構造函數#region?構造函數
????????/**////?<summary>
????????///?構造函數
????????///?</summary>
????????///?<param?name="pageIndex">當前頁(第一頁為0)</param>
????????///?<param?name="pageCount">總頁數</param>
????????///?<param?name="first">首頁</param>
????????///?<param?name="prev">上頁</param>
????????///?<param?name="next">下頁</param>
????????///?<param?name="last">尾頁</param>
????????///?<param?name="pageinfo">分頁信息</param>
????????///?<param?name="numcount">當前頁數字左右顯示的數字個數</param>
????????///?<param?name="isShowSmartPage">是否顯示智能導航數字</param>
????????///?<param?name="beiShu">智能導航數字的第一個的倍數</param>
????????///?<param?name="pageInfoShowLeft">是否左邊顯示分頁信息</param>
????????///?<param?name="smartPageColor">智能數字顏色</param>
????????///?<param?name="currentPageColor">當前頁顏色</param>
????????public?ExtendGridViewPagerTemplate(
????????????int?pageIndex,
????????????int?pageCount,
????????????string?first,
????????????string?prev,
????????????string?next,
????????????string?last,
????????????string?pageinfo,
????????????int?numcount,
????????????bool?isShowSmartPage,
????????????int?beiShu,
????????????bool?pageInfoShowLeft,
????????????string?smartPageColor,
????????????string?currentPageColor
????????????)
????????{
????????????_pageIndex?=?pageIndex;
????????????_pageCount?=?pageCount;
????????????_First?=?first;
????????????_Prev?=?prev;
????????????_Next?=?next;
????????????_Last?=?last;
????????????_PageInfo?=?pageinfo;
????????????_NumCount?=?numcount;
????????????_IsShowSmartPage?=?isShowSmartPage;
????????????_Beishu?=?beiShu;
????????????_PageInfoShowLeft?=?pageInfoShowLeft;
????????????_SmartPageColor?=?smartPageColor;
????????????_CurrentPageColor?=?currentPageColor;
????????}
????????#endregion
????????/**////?<summary>
????????///?對象初始化時調用?
????????///?</summary>
????????///?<param?name="container"></param>
????????public?void?InstantiateIn(Control?container)
????????{
????????????if?(_PageInfoShowLeft)
????????????{
????????????????createPageInfo(container);
????????????????createSpacer(container);
????????????}
????????????createFirstLinkButtion(container);//首頁
????????????createSpacer(container);//空格
????????????createPrevLinkButton(container);//上頁
????????????createSpacer(container);
????????????numericBar(container);//數字導航條
????????????createSpacer(container);
????????????createNextLinkButton(container);//下頁
????????????createSpacer(container);
????????????createLastLinkButtion(container);//尾頁
????????????if?(!_PageInfoShowLeft)
????????????{
????????????????createPageInfo(container);
????????????????createSpacer(container);
????????????}
????????}
????????//成生空格
????????private?void?createSpacer(Control?container)
????????{
????????????Literal?spacer?=?new?Literal();
????????????spacer?=?new?Literal();
????????????spacer.Text?=?" ";
????????????container.Controls.Add(spacer);
????????}
????????//生成首頁按鈕
????????private?void?createFirstLinkButtion(Control?container)
????????{
????????????if?(_pageIndex?>?0)
????????????{
????????????????LinkButton?pageButton;
????????????????pageButton?=?new?LinkButton();
????????????????pageButton.Text?=?_First;
????????????????pageButton.CommandName?=?"Page";
????????????????pageButton.CommandArgument?=?"1";
????????????????container.Controls.Add(pageButton);
????????????}
????????????else
????????????{
????????????????Literal?temp?=?new?Literal();
????????????????temp.Text?=?"<span?disabled=true>"?+?_First?+?"</span>";
????????????????container.Controls.Add(temp);
????????????}
????????}
????????//生成上一頁按鈕
????????private?void?createPrevLinkButton(Control?container)
????????{
????????????if?(_pageIndex?>?0)
????????????{
????????????????LinkButton?prevButton?=?new?LinkButton();
????????????????prevButton.CommandName?=?"Page";
????????????????prevButton.CommandArgument?=?"Prev";
????????????????prevButton.Text?=?_Prev;
????????????????container.Controls.Add(prevButton);
????????????}
????????????else
????????????{
????????????????Literal?temp?=?new?Literal();
????????????????temp.Text?=?"<span?disabled=true>"?+?_Prev?+?"</span>";
????????????????container.Controls.Add(temp);
????????????}
????????}
????????//生成下一頁按鈕
????????private?void?createNextLinkButton(Control?container)
????????{
????????????if?(_pageIndex?<?_pageCount?-?1?&&?_pageIndex?>=?0)
????????????{
????????????????LinkButton?prevButton?=?new?LinkButton();
????????????????prevButton.CommandName?=?"Page";
????????????????prevButton.CommandArgument?=?"Next";
????????????????prevButton.Text?=?_Next;
????????????????container.Controls.Add(prevButton);
????????????}
????????????else
????????????{
????????????????Literal?temp?=?new?Literal();
????????????????temp.Text?=?"<span?disabled=true>"?+?_Next?+?"</span>";
????????????????container.Controls.Add(temp);
????????????}
????????}
????????//生成尾按鈕
????????private?void?createLastLinkButtion(Control?container)
????????{
????????????if?(_pageIndex?<?_pageCount?-?1?&&?_pageIndex?>=?0)
????????????{
????????????????LinkButton?pageButton;
????????????????pageButton?=?new?LinkButton();
????????????????pageButton.Text?=?_Last;
????????????????pageButton.CommandName?=?"Page";
????????????????pageButton.CommandArgument?=?_pageCount.ToString();
????????????????container.Controls.Add(pageButton);
????????????}
????????????else
????????????{
????????????????Literal?temp?=?new?Literal();
????????????????temp.Text?=?"<span?disabled=true>"?+?_Last?+?"</span>";
????????????????container.Controls.Add(temp);
????????????}
????????}
????????//生成數字按鈕
????????private?void?createNumericPageButton(Control?container,?int?pageIndex,?bool?isSmatrPage)
????????{
????????????string?_text?=?(pageIndex?+?1).ToString();
????????????if?(isSmatrPage)
????????????{
????????????????_text?=?"<font?color=\""?+?this._SmartPageColor?+?"\">"?+?_text?+?"</font>";
????????????}
????????????if?(pageIndex?==?_pageIndex)
????????????{
????????????????_text?=?"<font?color=\""?+?this._CurrentPageColor?+?"\"><b>"?+?_text?+?"</b></font>";
????????????}
????????????LinkButton?pageButton;
????????????pageButton?=?new?LinkButton();
????????????pageButton.Text?=?_text;
????????????pageButton.CommandName?=?"Page";
????????????pageButton.CommandArgument?=?(pageIndex?+?1).ToString();
????????????container.Controls.Add(pageButton);
????????}
????????private?void?numericBar(Control?container)
????????{
????????????跟據?_NumCount?計算出導航條的?起始位置與結束位置#region?跟據?_NumCount?計算出導航條的?起始位置與結束位置
????????????//如果總頁數?小于或等于?導航條的數字個數,則把所有頁碼顯示出來
????????????if?(_pageCount?<=?_NumCount?*?2?+?1)
????????????{
????????????????_StartPage?=?1;
????????????????_EndPage?=?_pageCount;
????????????}
????????????else
????????????{
????????????????if?(_pageNumber?<=?_NumCount?+?1)
????????????????{
????????????????????_StartPage?=?1;
????????????????????_EndPage?=?_NumCount?*?2?+?1;
????????????????}
????????????????else
????????????????{
????????????????????if?(_pageCount?<=?_pageNumber?+?_NumCount)
????????????????????{
????????????????????????//顯示最后?x*2+1頁
????????????????????????_StartPage?=?_pageCount?-?_NumCount?*?2;
????????????????????????_EndPage?=?_pageCount;
????????????????????}
????????????????????else
????????????????????{
????????????????????????//正常顯示
????????????????????????_StartPage?=?_pageNumber?-?_NumCount;
????????????????????????_EndPage?=?_pageNumber?+?_NumCount;
????????????????????}
????????????????}
????????????}
????????????#endregion
????????????輸出前N頁#region?輸出前N頁
????????????if?(_IsShowSmartPage)
????????????{
????????????????_MoreStartPage1?=?_StartPage?-?_Beishu?-?1;
????????????????_MoreStartPage2?=?_StartPage?-?_Beishu?*?10?-?_Beishu?-?1;
????????????????_MoreStartPage3?=?_StartPage?-?_Beishu?*?100?-?_Beishu?*?10?-?_Beishu?-?1;
????????????????if?(_MoreStartPage1?>?0)
????????????????{
????????????????????if?(_MoreStartPage3?>?0)
????????????????????{
????????????????????????createNumericPageButton(container,?_MoreStartPage3,?true);
????????????????????????createSpacer(container);
????????????????????}
????????????????????if?(_MoreStartPage2?>?_NumCount?*?4)
????????????????????{
????????????????????????createNumericPageButton(container,?_MoreStartPage2,?true);
????????????????????????createSpacer(container);
????????????????????}
????????????????????else
????????????????????{
????????????????????????if?(_MoreStartPage2?>?0)
????????????????????????{
????????????????????????????createNumericPageButton(container,?_MoreStartPage2,?true);
????????????????????????????createSpacer(container);
????????????????????????}
????????????????????}
????????????????????createNumericPageButton(container,?_MoreStartPage1,?true);
????????????????????createSpacer(container);
????????????????}
????????????}
????????????#endregion
????????????輸出正常數字#region?輸出正常數字
????????????for?(int?i?=?_StartPage?-?1;?i?<?_EndPage;?i++)
????????????{
????????????????createNumericPageButton(container,?i,?false);
????????????????createSpacer(container);
????????????}
????????????#endregion
????????????輸出后N頁#region?輸出后N頁
????????????_MoreEndPage1?=?_EndPage?+?_Beishu?-?1;
????????????_MoreEndPage2?=?_EndPage?+?_Beishu?*?10?+?_Beishu?-?1;
????????????_MoreEndPage3?=?_EndPage?+?_Beishu?*?100?+?_Beishu?*?10?+?_Beishu?-?1;
????????????if?(_MoreEndPage1?<?_pageCount)
????????????{
????????????????createNumericPageButton(container,?_MoreEndPage1,?true);
????????????????createSpacer(container);
????????????????if?(_MoreEndPage2?<?_pageCount?-?_NumCount?*?4)
????????????????{
????????????????????createNumericPageButton(container,?_MoreEndPage2,?true);
????????????????????createSpacer(container);
????????????????????if?(_MoreEndPage3?<?_pageCount)
????????????????????{
????????????????????????createNumericPageButton(container,?_MoreEndPage3,?true);
????????????????????????createSpacer(container);
????????????????????}
????????????????}
????????????????else
????????????????{
????????????????????if?(_MoreEndPage2?<?_pageCount)
????????????????????{
????????????????????????createNumericPageButton(container,?_MoreEndPage1,?true);
????????????????????????createSpacer(container);
????????????????????}
????????????????}
????????????}
????????????#endregion
????????}
????????private?void?createPageInfo(Control?container)
????????{
????????????Literal?pageinfo?=?new?Literal();
????????????pageinfo?=?new?Literal();
????????????pageinfo.Text?=?_PageInfo;
????????????container.Controls.Add(pageinfo);
????????}
????}
}
?
public?partial?class?_Default?:?System.Web.UI.Page?{
????protected?void?Page_Load(object?sender,?EventArgs?e)
????{
????????if?(ViewState["SelectCmd"]?!=?null)
????????{
????????????SqlDataSource1.SelectCommand?=?(string)ViewState["SelectCmd"];
????????????MyGridView1.DataBind();
????????}
????????
????}
????protected?void?Button1_Click(object?sender,?EventArgs?e)
????{
????????ViewState["SelectCmd"]?=?"SELECT?[tid],?[fid],?[iconid],?[typeid]?FROM?[Forum_123_topics]?where?tid>10";
????????SqlDataSource1.SelectCommand?=?(string)ViewState["SelectCmd"];
????????MyGridView1.DataBind();
????????Response.Write(SqlDataSource1.SelectCommand);
????}
????protected?void?Button2_Click(object?sender,?EventArgs?e)
????{
????????ViewState["SelectCmd"]?=?"SELECT?[tid],?[fid],?[iconid],?[typeid]?FROM?[Forum_123_topics]";
????????SqlDataSource1.SelectCommand?=?(string)ViewState["SelectCmd"];
????????MyGridView1.DataBind();
????????Response.Write(SqlDataSource1.SelectCommand);
????}
}
轉載于:https://www.cnblogs.com/fanfajin/archive/2007/08/21/864566.html
總結
以上是生活随笔為你收集整理的GridView 简单扩展的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: syx278250658
- 下一篇: DBeaver连接GBase数据库