• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
内蒙古峰回路转armyfeng
博客园    首页    新随笔    联系   管理    订阅  订阅

自定义翻页的例子

  1using System;
  2using System.Collections;
  3using System.ComponentModel;
  4using System.Data;
  5using System.Drawing;
  6using System.Web;
  7using System.Web.SessionState;
  8using System.Web.UI;
  9using System.Web.UI.WebControls;
 10using System.Web.UI.HtmlControls;
 11using System.Data.SqlClient;
 12using System.Configuration;
 13using System.Web.Configuration;
 14using System.IO;
 15using System.Text;
 16
 17
 18public partial class _Default : System.Web.UI.Page
 19{
 20
 21    //private static string connString = System.ConfigurationSettings.AppSettings["ConnString"];//VS2003的用法
 22    private static string connString = System.Configuration.ConfigurationManager.AppSettings["ConnString"];//获取数据库连接
 23    private static string strSql = System.Configuration.ConfigurationManager.AppSettings["ConnStringSql"];//获取SQL执行语句
 24    private int recordCount;
 25    private int pageCount;
 26
 27    protected void Page_Load(object sender, System.EventArgs e)
 28    {
 29        // 在此处放置用户代码以初始化页面
 30        this.Label1.Visible = false;
 31        if (!Page.IsPostBack)
 32        {
 33            this.LBtnFirst.Enabled = false;
 34            this.LBtnPrev.Enabled = false;
 35            this.LBtnNext.Enabled = false;
 36            this.LBtnLast.Enabled = false;
 37            DataGridDataBind();
 38        }

 39    }

 40    //绑定数据
 41    private void DataGridDataBind()
 42    {
 43        DataSet ds = GetCustomersData();
 44        recordCount = ds.Tables[0].Rows.Count;
 45
 46        //获取当前的页数
 47        pageCount = (int)Math.Ceiling(recordCount * 1.0 / PageSize);
 48        //避免纪录从有到无时,并且已经进行过反页的情况下CurrentPageIndex > PageCount出错
 49        if (recordCount == 0)
 50        {
 51            this.DataGrid1.CurrentPageIndex = 0;
 52        }

 53        else if (this.DataGrid1.CurrentPageIndex >= pageCount)
 54        {
 55            this.DataGrid1.CurrentPageIndex = pageCount - 1;
 56        }

 57
 58        this.DataGrid1.DataSource = ds;
 59        this.DataGrid1.DataBind();
 60        NavigationStateChange();
 61    }

 62
 63    Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
 64    override protected void OnInit(EventArgs e)
 65    {
 66        //
 67        // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
 68        //
 69        InitializeComponent();
 70        base.OnInit(e);
 71    }

 72
 73    /**//// <summary>
 74    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
 75    /// 此方法的内容。
 76    /// </summary>

 77    private void InitializeComponent()
 78    {
 79
 80    }

 81    #endregion

 82    protected void LBtnNavigation_Click(object sender, System.EventArgs e)
 83    {
 84        LinkButton btn = (LinkButton)sender;
 85        switch (btn.CommandName)
 86        {
 87            case "First":
 88                PageIndex = 0;
 89                break;
 90            case "Prev"://if( PageIndex > 0 )
 91                PageIndex = PageIndex - 1;
 92                break;
 93            case "Next"://if( PageIndex < PageCount -1)
 94                PageIndex = PageIndex + 1;
 95                break;
 96            case "Last":
 97                PageIndex = PageCount - 1;
 98                break;
 99        }

100        DataGridDataBind();
101    }

102    public static DataSet GetCustomersData()
103    {
104        SqlConnection conn = new SqlConnection(connString);//执行SQL
105        SqlCommand comm = new SqlCommand(strSql, conn);
106        SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);
107
108        DataSet ds = new DataSet();
109        dataAdapter.Fill(ds);
110        return ds;
111    }

112
113    /**//// <summary>
114    /// 控制导航按钮或数字的状态
115    /// </summary>

116    public void NavigationStateChange()
117    {
118
119        if (PageCount <= 1)//( RecordCount <= PageSize )//小于等于一页
120        {
121
122            this.LBtnFirst.Enabled = false;
123            this.LBtnPrev.Enabled = false;
124            this.LBtnNext.Enabled = false;
125            this.LBtnLast.Enabled = false;
126
127        }

128        else //有多页
129        {
130
131            if (PageIndex == 0)//当前为第一页
132            {
133                this.LBtnFirst.Enabled = false;
134                this.LBtnPrev.Enabled = false;
135                this.LBtnNext.Enabled = true;
136                this.LBtnLast.Enabled = true;
137            }

138            else if (PageIndex == PageCount - 1)//当前为最后页 
139            {
140                this.LBtnFirst.Enabled = true;
141                this.LBtnPrev.Enabled = true;
142                this.LBtnNext.Enabled = false;
143                this.LBtnLast.Enabled = false;
144            }

145            else //中间页
146            {
147                this.LBtnFirst.Enabled = true;
148                this.LBtnPrev.Enabled = true;
149                this.LBtnNext.Enabled = true;
150                this.LBtnLast.Enabled = true;
151            }

152        }

153
154        if (RecordCount == 0)//当没有纪录时DataGrid.PageCount会显示1页
155
156            this.LtlPageCount.Text = "0";
157
158        else
159
160            this.LtlPageCount.Text = PageCount.ToString();
161
162        if (RecordCount == 0)
163
164            this.LtlPageIndex.Text = "0";
165
166        else
167
168            this.LtlPageIndex.Text = (PageIndex + 1).ToString();//在有页数的情况下前台显示页数加1
169        this.LtlPageSize.Text = PageSize.ToString();
170        this.LtlRecordCount.Text = RecordCount.ToString();
171    }

172
173    protected void Button1_Click(object sender, System.EventArgs e)
174    {
175        if (TextBox1.Text == string.Empty)
176        {
177            this.Label1.Visible = true;
178        }

179        else
180        {
181
182            this.Label1.Visible = false;
183            this.DataGrid1.CurrentPageIndex = 0;
184            //DropDownList1.ClearSelection();
185            string sDropDownList = DropDownList1.SelectedItem.Value;
186            string sTextBox = TextBox1.Text;
187            string sWhere = "";
188            switch (sDropDownList)
189            {
190                case "0":
191                    sWhere = " replace(Col002,' ','') like '%" + sTextBox + "%' order by Col007";
192                    break;
193                case "1":
194                    sWhere = " replace(Col004,' ','') like '%" + sTextBox + "%' order by Col007";
195                    break;
196                case "2":
197                    sWhere = " replace(Col005,' ','') like '%" + sTextBox + "%' order by Col007";
198                    break;
199            }

200
201            strSql = System.Configuration.ConfigurationManager.AppSettings["ConnStringSql"];
202            strSql = strSql + "where " + sWhere;
203
204            try
205            {
206                SqlConnection conn = new SqlConnection(connString);//执行SQL
207                SqlCommand comm = new SqlCommand(strSql, conn);
208                SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);
209
210                DataSet ds = new DataSet();
211                dataAdapter.Fill(ds);
212                DataGridDataBind();
213
214            }

215            catch (Exception ex)
216            {
217                Response.Write("数据库读写出错");
218                Response.Write(ex.Message);
219                Response.End();
220            }

221        }

222    }

223
224    public int PageCount
225    {
226        get { return this.DataGrid1.PageCount; }
227    }

228
229    //页大小
230
231    public int PageSize
232    {
233        get { return this.DataGrid1.PageSize; }
234    }

235
236    //页索引,从零开始
237    public int PageIndex
238    {
239        get { return this.DataGrid1.CurrentPageIndex; }
240        set { this.DataGrid1.CurrentPageIndex = value; }
241    }

242    // 纪录总数
243    public int RecordCount
244    {
245        get { return recordCount; }
246        set { recordCount = value; }
247    }

248}

249
posted @ 2006-08-09 16:52  老冯  阅读(288)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3