GridView自定义分页及其他示例

GridView 是一款功能强大的控件,也是在.Net WEB 开发中常用的控件之一。网上有关该控件的技术文章很多,这里也是借鉴了前人的做法,并在他们的基础上做了些改进,以更加贴近实际需求。同时也让自己对该控件更进一步的了解。

先看一下效果吧!

GridView 是一款功能强大的控件,也是在.Net WEB 开发中常用的控件之一。网上有关该控件的技术文章很多,这里也是借鉴了前人的做法,并在他们的基础上做了些改进,以更加贴近实际需求。同时也让自己对该控件更进一步的了解。

先看一下效果吧!

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using System.Text;


namespace GridViewPager
{
publicpartialclass Default : System.Web.UI.Page
{
DBHelper.DBHelper DB
=new DBHelper.DBHelper();
protectedvoid Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
Gw_Bind();
}
}
publicvoid Gw_Bind()
{
string sql ="select * from tb_News order by ID ";
DataTable dt
= DB.GetData(sql, "tb_News");
GwPage.DataSource
= dt;
// GwPage.DataKeyNames = new string[] { "NewsTitle" };//设置一个主键
GwPage.DataKeyNames =newstring[] {"ID"};//设置一个主键
GwPage.DataBind();
}
//GridView 控件页号选择发生改变时激发此事件实现显示数据更新刷新
protectedvoid GwPage_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//分页完成之前
GwPage.PageIndex = e.NewPageIndex;
Gw_Bind();
}
#region 分页
//创建一个页码导航
protectedvoid IndexNavigator(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.Pager)
{
PlaceHolder ph
= (PlaceHolder)e.Row.FindControl("numeric");
int StartIndex = (GwPage.PageIndex) /10*10+1;//根据当前页得到第一个页码
int EndIndex = StartIndex +9;//每页显示10条记录
if(EndIndex > GwPage.PageCount)//判断是否是最后一页
{
EndIndex
= GwPage.PageCount;
}
//循环动态显示页码
for (int i = StartIndex; i <= EndIndex;i++ )
{
var lbtn
=new LinkButton {CommandName ="Page",CommandArgument = i.ToString(),Text ="["+i+"]" };
ph.Controls.Add(i
-1!= GwPage.PageIndex?(Control)lbtn:new LiteralControl(i.ToString()));
ph.Controls.Add(
new LiteralControl(""));
}
}
}
//实现各页面切换
protectedvoid RowSwich(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName =="Page")
{
switch(e.CommandArgument.ToString())
{
case"First": //首页
GwPage.PageIndex =0;
break;
case"Prev": //前翻
if(GwPage.PageIndex >0)
{
GwPage.PageIndex
-=1;
}
break;
case"Next": //后翻
if(GwPage.PageIndex < GwPage.PageCount-1)
{
GwPage.PageIndex
+=1;
}
break;
case"Last": //尾页
if(GwPage.PageIndex >= GwPage.PageCount)
{
GwPage.PageIndex
= GwPage.PageCount -1;
}
break;
default:
GwPage.PageIndex
= Int32.Parse(e.CommandArgument.ToString()) -1;
break;
}
}
}
#endregion
//删除一行
protectedvoid GwPage_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int s =int.Parse(GwPage.DataKeys[e.RowIndex].Value.ToString());//获取主键值
string sql ="delete from tb_News where ID = '"+ s +"'";
int result = DB.DelRowByOne(sql);
Response.Write(
"<script>alert('删除"+ result+"条记录!')</script>");
Gw_Bind();
}
//编辑一行
protectedvoid GwPage_RowEditing(object sender, GridViewEditEventArgs e)
{
GwPage.EditIndex
= e.NewEditIndex;
Gw_Bind();
}
//更新一行
protectedvoid GwPage_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int s =int.Parse(GwPage.DataKeys[e.RowIndex].Value.ToString());//获取主键值
string NewsTitle = ((TextBox)(this.GwPage.Rows[e.RowIndex].Cells[2].Controls[0])).Text;
string NewsContent = ((TextBox)(this.GwPage.Rows[e.RowIndex].Cells[3].Controls[0])).Text;
DateTime FBdate
= DateTime.Parse(((TextBox)(this.GwPage.Rows[e.RowIndex].Cells[4].Controls[0])).Text);
string sql ="update tb_News set NewsTitle = '"+ NewsTitle +"',NewsContent = '"+ NewsContent +"',FBDate = '"+ FBdate +"' where ID = '"+ s +"'";
int result = DB.UpdateRowByOne(sql);
Response.Write(
"<script>alert('更新"+ result +"条记录!')</script>");
Gw_Bind();
}
//取消编辑
protectedvoid GwPage_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GwPage.EditIndex =-1;
Gw_Bind();
}
//批量删除
protectedvoid btnDel_Click(object sender, EventArgs e)
{
int result =0;
//循环判断checkBox的选中情况
for (int i =0; i < GwPage.Rows.Count;i++ )
{
CheckBox ck
= (CheckBox)GwPage.Rows[i].FindControl("CheckBox1");
if(ck.Checked) //若选中执行删除操作
{
//int s = Convert.ToInt32(gvTest.DataKeys[i].Value);
int s =int.Parse(GwPage.DataKeys[i].Value.ToString());//获取主键值
string sql ="delete from tb_News where ID = '"+ s +"'";
result
= DB.DelRowByOne(sql);
result
+= result;
}
}
Gw_Bind();
Response.Write(
"<script>alert('批量删除"+ result +"条记录!')</script>");
}
//保存页面为EXCEL
protectedvoid PageToExcel_Click(object sender, EventArgs e)
{

Response.Clear();
Response.Buffer
=true;
Response.AppendHeader(
"Content-Disposition", "attachment;filename=FileName.xls");
Response.ContentEncoding
= System.Text.Encoding.UTF8;
Response.ContentType
="application/vnd.ms-excel";
this.EnableViewState =false;
}
//导出GridView数据到Excel表中
publicoverridevoid VerifyRenderingInServerForm(Control control)
{

}
protectedvoid GridToExcel_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader(
"content-disposition","attachment;filename=FileName.xls");
Response.Charset
="gb2312";
Response.ContentType
="application/vnd.ms-excel";//设置文件输出类型为Excel文件
StringWriter sw =new StringWriter();
HtmlTextWriter htmlWrite
=new HtmlTextWriter(sw);
GwPage.AllowPaging
=false;
Gw_Bind();
GwPage.RenderControl(htmlWrite);
Response.Write(sw.ToString());
Response.End();
GwPage.AllowPaging
=true;
Gw_Bind();
}
//插入一条记录
protectedvoid btnInsert_Click(object sender, EventArgs e)
{
string NewsTitle = txtNewsTitle.Text;
string NewsContent = txtNewsContent.Text;
DateTime FBDate
= DateTime.Parse(txtFBDate.Text);
string sql ="insert into tb_News(NewsTitle,NewsContent,FBDate)"
+"values('"+ NewsTitle +"','"+ NewsContent +"','"+ FBDate +"')";
int result = DB.InSertRowByOne(sql);
Gw_Bind();
Response.Write(
"<script>alert('插入"+ result +"条记录!')</script>");
}
}
}

下载示例链接:GridViewPager.rar

posted @ 2011-09-13 13:11  楠木大叔  阅读(367)  评论(0编辑  收藏  举报