[新闻系统]自定义分页控件(针对MS SQL数据库)

本文介绍了一个针对MSSQL数据库的自定义分页控件实现,通过SQL语句进行数据查询与分页处理,支持多种.NET控件的分页显示,并提供了完整的源代码。
参考了MSDN的分页控件  他写的太麻烦也用不到那么多东西,对于这个新闻系统来讲也没必要做那么麻烦
参考地址:http://www.microsoft.com/china/msdn/archives/library/dnaspp/html/PagerControls.asp

针对MS SQL数据库 采用SQL语句查询

下载http://files.cnblogs.com/zhzkl/Control.rar
代码贴出来
None.gifusing System;
None.gif
using System.Web;
None.gif
using System.Web.UI;
None.gif
using System.Web.UI.WebControls;
None.gif
using System.ComponentModel;
None.gif
using System.Data.SqlClient;
None.gif
using System.Data;
None.gif
None.gif
namespace zhzklControls
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// WebCustomControl1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    [DefaultProperty("Text"),
InBlock.gif        ToolboxData(
"<{0}:ControlTest runat=server></{0}:ControlTest>")]
InBlock.gif    
public class ControlTest : System.Web.UI.WebControls.WebControl , INamingContainer
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public class PageChangedEventArgs : EventArgs
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
public int OldPageIndex;
InBlock.gif            
public int NewPageIndex;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
private string text;
InBlock.gif        
private Control _controlToPaginate = null;
InBlock.gif        
private string QueryPageCommandText = "SELECT * FROM " + 
InBlock.gif            
"(SELECT TOP {0} * FROM " + 
InBlock.gif            
"(SELECT TOP {1} * FROM ({2}) AS t0 ORDER BY {3} {4}) AS t1 " + 
InBlock.gif            
"ORDER BY {3} {5}) AS t2 " + 
InBlock.gif            
"ORDER BY {3}";
InBlock.gif        
private string QueryCountCommandText = "SELECT COUNT(*) FROM ({0}) AS t0";
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Programme Interface#region Programme Interface        
InBlock.gif        [Bindable(
true),Category("Appearance"),DefaultValue("")]
InBlock.gif        
public string Text
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return text;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                text 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string Test()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return "欢迎来到自定义控件的世界";
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Description(
"表中的排序字段")]
InBlock.gif        
public string SortField
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gif{return Convert.ToString(ViewState["SortKeyField"]);}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ViewState["SortKeyField"= value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string ConnectionString
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn Convert.ToString(ViewState["ConnectionString"]); }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ ViewState["ConnectionString"= value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public string SelectCommand
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn Convert.ToString(ViewState["SelectCommand"]); }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ ViewState["SelectCommand"= value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public int PageSize
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn Convert.ToInt32(ViewState["PageSize"]); }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ ViewState["PageSize"= value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private int TotalPages
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn Convert.ToInt32(ViewState["TotalPages"]); }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ ViewState["TotalPages"= value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public int CurrentPageIndex
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gif{return Convert.ToInt32(ViewState["CurrentPageIndex"]);}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ViewState["CurrentPageIndex"= value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public delegate void PageChangedEventHandler(object sender, PageChangedEventArgs e);
InBlock.gif        
public event PageChangedEventHandler PageIndexChanged;//呵呵 第一次看到代理与事件的应用
InBlock.gif
        protected virtual void OnPageIndexChanged(PageChangedEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (PageIndexChanged != null)
InBlock.gif                PageIndexChanged(
this, e);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string ControlToPaginate
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn Convert.ToString(ViewState["ControlToPaginate"]); }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ ViewState["ControlToPaginate"= value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 执行数据绑定,覆盖了基类的数据绑定
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public override void DataBind()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.DataBind();
InBlock.gif            ChildControlsCreated 
= false;
InBlock.gif
InBlock.gif            _controlToPaginate 
= Page.FindControl(ControlToPaginate);
InBlock.gif
InBlock.gif            TotalPages 
= GetPageCount();
InBlock.gif
InBlock.gif            BaseDataList baseDataListControl 
= null;
InBlock.gif            ListControl listControl 
= null;
InBlock.gif            Repeater repeater 
= null;
InBlock.gif            
if(_controlToPaginate is Repeater)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                repeater 
= (Repeater) _controlToPaginate;
InBlock.gif                repeater.DataSource 
= FetchPageData();
InBlock.gif                repeater.DataBind();            
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (_controlToPaginate is BaseDataList)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                baseDataListControl 
= (BaseDataList) _controlToPaginate;
InBlock.gif                baseDataListControl.DataSource 
= FetchPageData(); 
InBlock.gif                baseDataListControl.DataBind();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (_controlToPaginate is ListControl)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                listControl 
= (ListControl) _controlToPaginate;
InBlock.gif                listControl.Items.Clear(); 
InBlock.gif                listControl.DataSource 
= FetchPageData(); 
InBlock.gif                listControl.DataBind();
ExpandedSubBlockEnd.gif            }
        
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
UserInterface#region UserInterface    
InBlock.gif        
private void BuildUI()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
bool CanMoveBack = CurrentPageIndex > 0;
InBlock.gif            
bool CanMoveNext = CurrentPageIndex < TotalPages - 1;
InBlock.gif
InBlock.gif            Table t 
= new Table();
InBlock.gif            t.Font.Name 
= Font.Name;
InBlock.gif            t.Font.Size 
= Font.Size;
InBlock.gif            t.BorderStyle 
= BorderStyle;
InBlock.gif            t.BorderWidth 
= BorderWidth;
InBlock.gif            t.BorderColor 
= BorderColor;
InBlock.gif            t.Width 
= Width;
InBlock.gif            t.Height 
= Height;
InBlock.gif            t.BackColor 
= BackColor;
InBlock.gif            t.ForeColor 
= ForeColor;
InBlock.gif
InBlock.gif            
// Build the table row
InBlock.gif
            TableRow row = new TableRow();
InBlock.gif            t.Rows.Add(row);
InBlock.gif            
InBlock.gif            
// Build the cell with navigation bar
InBlock.gif
            TableCell cell = new TableCell();
InBlock.gif            LinkButton first 
= new LinkButton();
InBlock.gif            first.ID 
= "First";
InBlock.gif            first.Click 
+= new EventHandler(first_Click);
InBlock.gif            first.ForeColor 
= ForeColor;
InBlock.gif            first.ToolTip 
= "第一页";
InBlock.gif            first.Text 
= "第一页";
InBlock.gif            first.Enabled 
= CanMoveBack;
InBlock.gif            cell.Controls.Add(first);
InBlock.gif
InBlock.gif            
// Add a separator
InBlock.gif
            cell.Controls.Add(new LiteralControl("&nbsp;"));
InBlock.gif
InBlock.gif            
// Render the < button
InBlock.gif
            LinkButton prev = new LinkButton();
InBlock.gif            prev.ID 
= "Prev";
InBlock.gif            prev.Click 
+=new EventHandler(prev_Click);
InBlock.gif            prev.ForeColor 
= ForeColor;
InBlock.gif            prev.ToolTip 
= "上一页";
InBlock.gif            prev.Text 
= "上一页";
InBlock.gif            prev.Enabled 
= CanMoveBack;
InBlock.gif            cell.Controls.Add(prev);
InBlock.gif
InBlock.gif            
// Add a separator
InBlock.gif
            cell.Controls.Add(new LiteralControl("&nbsp;"));
InBlock.gif            
InBlock.gif            
// Render the > button
InBlock.gif
            LinkButton next = new LinkButton();
InBlock.gif            next.ID 
= "Next";
InBlock.gif            next.Click 
+= new EventHandler(next_Click);
InBlock.gif            next.ForeColor 
= ForeColor;
InBlock.gif            next.ToolTip 
= "下一页";
InBlock.gif            next.Text 
= "下一页";    
InBlock.gif            next.Enabled 
= CanMoveNext;
InBlock.gif            cell.Controls.Add(next);
InBlock.gif
InBlock.gif            
// Add a separator
InBlock.gif
            cell.Controls.Add(new LiteralControl("&nbsp;"));
InBlock.gif
InBlock.gif            
// Render the >> button
InBlock.gif
            LinkButton last = new LinkButton();
InBlock.gif            last.ID 
= "Last";
InBlock.gif            last.Click 
+= new EventHandler(last_Click);
InBlock.gif            last.ForeColor 
= ForeColor;
InBlock.gif            last.ToolTip 
= "最后一页";
InBlock.gif            last.Text 
= "最后一页";
InBlock.gif            last.Enabled 
= CanMoveNext;
InBlock.gif            cell.Controls.Add(last);
InBlock.gif
InBlock.gif            cell.Controls.Add(
new LiteralControl("&nbsp;&nbsp;"));
InBlock.gif
InBlock.gif            
// 跳转
InBlock.gif
            cell.Controls.Add(new LiteralControl("转到"));
InBlock.gif
InBlock.gif            DropDownList dpPageIndex 
= new DropDownList();
InBlock.gif            dpPageIndex.ID 
= "dpPageIndex";
InBlock.gif            dpPageIndex.AutoPostBack 
= true;
InBlock.gif            dpPageIndex.ForeColor 
= ForeColor;
InBlock.gif            
for(int i=1; i<=TotalPages; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ListItem item 
= new ListItem(i.ToString(), (i-1).ToString());
InBlock.gif                dpPageIndex.Items.Add(item);
ExpandedSubBlockEnd.gif            }

InBlock.gif            dpPageIndex.SelectedIndex 
= CurrentPageIndex;
InBlock.gif            dpPageIndex.SelectedIndexChanged 
+= new EventHandler(dpPageIndex_SelectedIndexChanged);
InBlock.gif            cell.Controls.Add(dpPageIndex);
InBlock.gif
InBlock.gif            cell.Controls.Add(
new LiteralControl("页&nbsp;共"));
InBlock.gif
InBlock.gif            Literal lt 
= new Literal();
InBlock.gif            lt.Text 
= TotalPages.ToString();
InBlock.gif            cell.Controls.Add(lt);
InBlock.gif
InBlock.gif            cell.Controls.Add(
new LiteralControl(""));
InBlock.gif
InBlock.gif            row.Cells.Add(cell);
InBlock.gif            Controls.Add(t);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
HelpMethod#region HelpMethod 
InBlock.gif        
protected DataTable FetchPageData()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlConnection conn 
= new SqlConnection(ConnectionString);
InBlock.gif            SqlDataAdapter dp 
= new SqlDataAdapter();
InBlock.gif
InBlock.gif            
int recordsToRec = PageSize;
InBlock.gif            
int lastPageRecord = PageSize;
InBlock.gif            
if( CurrentPageIndex == TotalPages - 1
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if((GetQueryVirtualCount()%PageSize) > 0)
InBlock.gif                    lastPageRecord 
= this.GetQueryVirtualCount() % PageSize;
InBlock.gif
InBlock.gif                recordsToRec 
= lastPageRecord;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            
string cmdText = String.Format(QueryPageCommandText, 
InBlock.gif                recordsToRec,                            
// {0} --> page size
InBlock.gif
                PageSize*(CurrentPageIndex+1),        // {1} --> size * index
InBlock.gif
                SelectCommand,                        // {2} --> base query
InBlock.gif
                SortField,                            // {3} --> key field in the query
InBlock.gif
                "ASC",                                // Default to ascending order
InBlock.gif
                "DESC");
InBlock.gif            dp.SelectCommand 
= new SqlCommand(cmdText,conn);
InBlock.gif
InBlock.gif            DataTable dt 
= new DataTable();
InBlock.gif            dp.Fill(dt);
InBlock.gif
InBlock.gif            
return dt;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected void GoToPage(int pageIndex)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// Prepares event data
InBlock.gif
            PageChangedEventArgs e = new PageChangedEventArgs();
InBlock.gif            e.OldPageIndex 
= CurrentPageIndex;
InBlock.gif            e.NewPageIndex 
= pageIndex;
InBlock.gif
InBlock.gif            
// Updates the current index
InBlock.gif
            CurrentPageIndex = pageIndex;
InBlock.gif            DropDownList dpPageIndex 
= (DropDownList)this.FindControl("dpPageIndex");
InBlock.gif            dpPageIndex.SelectedIndex 
= pageIndex;
InBlock.gif
InBlock.gif            
// Fires the page changed event
InBlock.gif
            OnPageIndexChanged(e);
InBlock.gif
InBlock.gif            
this.DataBind();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private int GetPageCount()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return (GetQueryVirtualCount()%PageSize) == 0?(GetQueryVirtualCount()/PageSize):(GetQueryVirtualCount()/PageSize) + 1;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private int GetQueryVirtualCount()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string cmdText = String.Format(QueryCountCommandText, SelectCommand);
InBlock.gif            SqlConnection conn 
= new SqlConnection(ConnectionString);
InBlock.gif            SqlCommand cmd 
= new SqlCommand(cmdText, conn);
InBlock.gif
InBlock.gif            cmd.Connection.Open();
InBlock.gif            
int recCount = (int) cmd.ExecuteScalar(); 
InBlock.gif            cmd.Connection.Close();
InBlock.gif
InBlock.gif            
return recCount;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion
 
InBlock.gif
InBlock.gif        
private void first_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            GoToPage(
0);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void prev_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            GoToPage(
this.CurrentPageIndex - 1);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void next_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            GoToPage(
this.CurrentPageIndex + 1);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void last_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            GoToPage(
this.TotalPages - 1);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void CreateChildControls()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Controls.Clear();
InBlock.gif            ClearChildViewState();
InBlock.gif
InBlock.gif            BuildUI();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 将此控件呈现给指定的输出参数。    
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="output"> 要写出到的 HTML 编写器 </param>

InBlock.gif        protected override void Render(HtmlTextWriter output)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (Site != null && Site.DesignMode) 
InBlock.gif                CreateChildControls();            
InBlock.gif            
InBlock.gif            
base.Render(output);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void dpPageIndex_SelectedIndexChanged(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DropDownList pageList 
= (DropDownList) sender;
InBlock.gif            
int pageIndex = Convert.ToInt32(pageList.SelectedItem.Value);
InBlock.gif            GoToPage(pageIndex);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/zhzkl/archive/2006/02/09/327873.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值