热烈祝贺台州朗动科技的站长论坛隆重上线!(2012-05-28)    热烈庆祝伟大的祖国60周年生日 点击进来我们一起为她祝福吧(2009-09-26)    站长论坛禁止发布广告,一经发现立即删除。谢谢各位合作!.(2009-08-08)    热烈祝贺台州网址导航全面升级,全新版本上线!希望各位一如既往地支持台州网址导航的发展.(2009-03-28)    台州站长论坛恭祝各位新年快乐,牛年行大运!(2009-01-24)    台州Link正式更名为台州网址导航,专业做以台州网址为主的网址导航!(2008-05-23)    热烈祝贺台州Link资讯改名为中国站长资讯!希望在以后日子里得到大家的大力支持和帮助!(2008-04-10)    热烈祝贺台州Link论坛改名为台州站长论坛!希望大家继续支持和鼓励!(2008-04-10)    台州站长论坛原[社会琐碎]版块更名为[生活百科]版块!(2007-09-05)    特此通知:新台州站长论坛的数据信息全部升级成功!">特此通知:新台州站长论坛的数据信息全部升级成功!(2007-09-01)    台州站长论坛对未通过验证的会员进行合理的清除,请您谅解(2007-08-30)    台州网址导航|上网导航诚邀世界各地的网站友情链接和友谊联盟,共同引领网站导航、前进!(2007-08-30)    禁止发广告之类的帖,已发现立即删除!(2007-08-30)    希望各位上传与下载有用资源和最新信息(2007-08-30)    热烈祝贺台州站长论坛全面升级成功,全新上线!(2007-08-30)    
便民网址导航,轻松网上冲浪。
台州维博网络专业开发网站门户平台系统
您当前的位置: 首页 » ASP/ASP.NET编程 » 使用DataGrid实现自定分页的例子

使用DataGrid实现自定分页的例子

论坛链接
  • 使用DataGrid实现自定分页的例子
  • 发布时间:2007-09-19 23:45:49    浏览数:7002    发布者:webptr    设置字体【   
MSSQL中建立一张表t_dict,包括四个字段FDictid(int),FNameCn(varchar),FNameEn(varchar),FNote(text)




<body>
<form id="DictList" method="post" runat="server">
<TABLE style="BORDER-COLLAPSE: collapse" cellSpacing="0" width="100%" border="1">
<TR>
<td bgColor="#c0c000">信息:<FONT face="Arial" color="#ffffff">数据维护</FONT>
</td>
</TR>
<tr>
<td><FONT face="宋体"></FONT></td>
</tr>
<tr>
<td><asp:datagrid id="MyDataGrid" runat="server" DataKeyField="FDictid" AutoGenerateColumns="False"
AllowPaging="True" Width="100%">
<SelectedItemStyle BackColor="#FFC080"></SelectedItemStyle>
<HeaderStyle BackColor="#C0C000"></HeaderStyle>
<Columns>
<asp:ButtonColumn Text="选择" HeaderText="选择" CommandName="Select">
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" Width="8%"></HeaderStyle>
<ItemStyle Font-Bold="True" HorizontalAlign="Center"></ItemStyle>
</asp:ButtonColumn>
<asp:BoundColumn DataField="FDictID" HeaderText="标识号">
<HeaderStyle Width="15%"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="FNameCn" HeaderText="名称">
<HeaderStyle Width="15%"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="FNameEn" HeaderText="英文名称">
<HeaderStyle Width="15%"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="FNote" HeaderText="描叙">
<HeaderStyle Width="47%"></HeaderStyle>
</asp:BoundColumn>
</Columns>
<PagerStyle Visible="False"></PagerStyle>
</asp:datagrid></td>
</tr>
</TABLE>
<TABLE style="BORDER-COLLAPSE: collapse" cellSpacing="0" width="100%" bgColor="#ff9966"
border="1">
<TR>
<td align="right"><asp:linkbutton id="btnFirst" runat="server" CommandArgument="fist">首页</asp:linkbutton>
<asp:linkbutton id="btnPrev" runat="server" CommandArgument="prev">上一页</asp:linkbutton>
<asp:linkbutton id="btnNext" runat="server" CommandArgument="next">下一页</asp:linkbutton>
<asp:linkbutton id="btnLast" runat="server" CommandArgument="last">末页</asp:linkbutton>
<asp:label id="lblCurrentIndex" runat="server"></asp:label>/<asp:label id="lblPageCount" runat="server"></asp:label>
跳转到<asp:textbox id="txtGoPage" runat="server" Width="30px" CssClass="textbox"></asp:textbox>
<asp:button id="btnGo" runat="server" Width="29px" CssClass="button" Text="GO"></asp:button></td>
</TR>
</TABLE>
</form>
</body>





using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace CrystalReport
{
/// <summary>
/// DataGridcustom 的摘要说明。
/// </summary>
public class DataGridcustom : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid MyDataGrid;
protected System.Web.UI.WebControls.LinkButton btnFirst;
protected System.Web.UI.WebControls.LinkButton btnPrev;
protected System.Web.UI.WebControls.LinkButton btnNext;
protected System.Web.UI.WebControls.LinkButton btnLast;
protected System.Web.UI.WebControls.Label lblCurrentIndex;
protected System.Web.UI.WebControls.Label lblPageCount;
protected System.Web.UI.WebControls.TextBox txtGoPage;
protected System.Web.UI.WebControls.Button btnGo;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (!IsPostBack)
BindGrid();
}

public void BindGrid()
{
SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnString"]);
string strSql ="SELECT * FROM t_dict order by FDictID desc";
SqlDataAdapter myCommand = new SqlDataAdapter(strSql, myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds, "t_dict");
MyDataGrid.DataSource=ds.Tables["t_dict"].DefaultView;
MyDataGrid.DataBind();
ShowStatsPage();
}

private void PagerButtonClick(object sender, System.EventArgs e)
{
//获得LinkButton的参数值
String arg = ((LinkButton)sender).CommandArgument;

switch(arg)
{
case ("next"):
if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1))
MyDataGrid.CurrentPageIndex ++;
break;
case ("prev"):
if (MyDataGrid.CurrentPageIndex > 0)
MyDataGrid.CurrentPageIndex --;
break;
case ("fist"):
MyDataGrid.CurrentPageIndex=0;
break;
case ("last"):
MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1);
break;
default:
//本页值
MyDataGrid.CurrentPageIndex = Convert.ToInt32(arg);
break;
}
BindGrid();
}

void ShowStatsPage()
{
//显示页面信息
lblCurrentIndex.Text = "[<font color=\"blue\">当前为第:" + ((int)MyDataGrid.CurrentPageIndex+1) + "页</font>]";
lblPageCount.Text = "[<font color=\"blue\">共:" + MyDataGrid.PageCount + "页</font>]";
}

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

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.MyDataGrid.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.MyDataGrid_PageIndexChanged);
this.btnFirst.Click += new System.EventHandler(this.PagerButtonClick);
this.btnPrev.Click += new System.EventHandler(this.PagerButtonClick);
this.btnNext.Click += new System.EventHandler(this.PagerButtonClick);
this.btnLast.Click += new System.EventHandler(this.PagerButtonClick);
this.btnGo.Click += new System.EventHandler(this.btnGo_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void MyDataGrid_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
//处理按下数字的方法
MyDataGrid.CurrentPageIndex = e.NewPageIndex;
BindGrid();

}

private void btnGo_Click(object sender, System.EventArgs e)
{
//页面直接跳转的代码
if(txtGoPage.Text.Trim()!="")
{
int PageI=Int32.Parse(txtGoPage.Text.Trim())-1;
if (PageI >=0 && PageI < (MyDataGrid.PageCount))
MyDataGrid.CurrentPageIndex = PageI ;
}
BindGrid();

}

private void btnFirst_Click(object sender, System.EventArgs e)
{

}

private void btnPrev_Click(object sender, System.EventArgs e)
{

}

private void btnNext_Click(object sender, System.EventArgs e)
{

}

private void btnLast_Click(object sender, System.EventArgs e)
{

}
}
}
娱乐休闲专区A 影视预告B 音乐咖啡C 英语阶梯D 生活百科
网页编程专区E AMPZF HTMLG CSSH JSI ASPJ PHPK JSPL MySQLM AJAX
Linux技术区 N 系统管理O 服务器架设P 网络/硬件Q 编程序开发R 内核/嵌入
管理中心专区S 发布网址T 版主议事U 事务处理