gridview排序和分页

前台代码如下:

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title ></ title >
< style type ="text/css" >

body
{ width : 800px ; height : 100px ; margin : 0px auto ; margin-bottom : 20px ; background-color : #F0FBEB ; }
span
{ color : Maroon ; }
.div
{ margin-top : 80px ; text-align : center }
.table
{ border-collapse : collapse ; width : 600px ; height : 100px ; margin : 0px auto ; margin-bottom : 20px ; border : 1px solid #BBE1F1 ; background-color : #EEFAFF }
.table th
{ margin : 1px ; background-color : #E2EAF8 ; height : 24px ; width : 14% ; color : Maroon ; border-right : 1px solid #CCEFF5 ; border-bottom : 1px solid #CCEFF5 ; }
.table td
{ border-right : 1px solid #CCEFF5 ; border-bottom : 1px solid #CCEFF5 ; }

</ style >
</ head >
< body >
< form id ="form1" runat ="server" >
< div class ="div " >
< asp:GridView ID ="GridView1" runat ="server" AutoGenerateColumns ="False"
onrowcommand
="GridView1_RowCommand" AllowPaging ="True" AllowSorting ="True"
onpageindexchanging
="GridView1_PageIndexChanging"
PageSize
="5" GridLines ="None" >
< Columns >
< asp:TemplateField >
< HeaderTemplate >
< table class ="table " >
< tr >
< th > 相片 </ th >
< th >< asp:LinkButton ID ="LinkButton3" CommandName ="number" runat ="server" >< span > 读者编号 </ span ></ asp:LinkButton ></ th >
< th >< asp:LinkButton ID ="LinkButton4" CommandName ="name" runat ="server" >< span > 读者姓名 </ span ></ asp:LinkButton ></ th >
< th >< asp:LinkButton ID ="LinkButton5" CommandName ="xingb" runat ="server" >< span > 性别 </ span ></ asp:LinkButton ></ th >
< th >< asp:LinkButton ID ="LinkButton6" CommandName ="tel" runat ="server" >< span > 电话 </ span ></ asp:LinkButton ></ th >
< th >< asp:LinkButton ID ="LinkButton7" CommandName ="tushujynum" runat ="server" >< span > 图书借阅次数 </ span ></ asp:LinkButton ></ th >
< th > 操作 </ th >
</ tr >
</ HeaderTemplate >
< ItemTemplate >
< tr >
< td >< img alt ="" src ='readImage.aspx?id=<%#Eval("number") % > ' width ="40px" height ="40px" /> <% -- 此处是显示数据库中的二进制图片 代码省略 -- %> </ td >
< td > <% # Eval ( " number " ) %> </ td >
< td > <% # Eval ( " name " ) %> </ td >
< td > <% # Eval ( " xingb " ) %> </ td >
< td > <% # Eval ( " tel " ) %> </ td >
< td > <% # Eval ( " tushujynum " ) %> </ td >
< td >
< asp:LinkButton ID ="LinkButton1" CommandName ="deletee" CommandArgument ='<%#Eval("number") % > ' runat="server">删除 </ asp:LinkButton >
< asp:LinkButton ID ="LinkButton2" CommandName ="updatee" CommandArgument ='<%#Eval("number") % > ' runat="server">修改 </ asp:LinkButton >
</ td >
</ tr >
</ ItemTemplate >
< FooterTemplate >
</ table >
</ FooterTemplate >

</ asp:TemplateField >
</ Columns >
</ asp:GridView >
</ div >
</ form >
</ body >
</ html >

 页面后台代码如下: 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebPricatice
{
public partial class gridview : System.Web.UI.Page
{
BLL.Test1 bll
= new BLL.Test1(); // 实例化业务逻辑层的Test1的方法
protected void Page_Load( object sender, EventArgs e)
{
if ( ! IsPostBack)
{
GridView1.Attributes.Add(
" SortExpression " , " number " );
GridView1.Attributes.Add(
" SortDirection " , " ASC " );
BindGrid();
}
}

public void BindGrid()
{
// 获取GridView排序数据列及排序方向
string sortExpression = GridView1.Attributes[ " SortExpression " ];
string sortDirection = GridView1.Attributes[ " SortDirection " ];

DataTable dt
= bll.getList().Tables[ 0 ]; // bll.getlist()是得到DataSet数据集的一个方法----代码已省略
// dt包括的字段有"number,name,xingb,tel,tusjynum等";
dt.DefaultView .Sort = string .Format( " {0} {1} " , sortExpression, sortDirection);
// 给gridview绑定数据
GridView1.DataSource = dt;
GridView1.DataBind();
}

protected void GridView1_RowCommand( object sender, GridViewCommandEventArgs e)
{
switch (e.CommandName)
{
case " deletee " :
// 删除id为e.CommandArgument的项
// Response.Write("<script>alert(\"删除'" + e.CommandArgument + "'\")</script>");
break ;
case " updatee " :
// 修改id为e.CommandArgument的项----代码省略
// Response.Write("alert(\"修改'" + e.CommandArgument + "'\")");
break ;
case " number " :
bindgridsort(
" number " );
break ;
case " name " :
bindgridsort(
" name " );
break ;
case " xingb " :
bindgridsort(
" xingb " );
break ;
case " tel " :
bindgridsort(
" tel " );
break ;
case " tushujynum " :
bindgridsort(
" tushujynum " );
break ;
default : break ;
}
}

/// <summary>
/// 得到gridview排序的顺序
/// </summary>
/// <param name="fieldstr"> 你要排序的字段名称 </param>
public void bindgridsort( string fieldstr)
{
// 错误的属性设置方法:SortExpression、SortDirection均是GridView只读属性,无法直接赋值。
// this.GridView1.SortExpression = "id";
// this.GridView1.SortDirection = "ASC";

string sortDirection = " ASC " ;
if (fieldstr == GridView1.Attributes[ " SortExpression " ])
{
// 获得下一次的排序状态
sortDirection = GridView1.Attributes[ " SortDirection " ].ToString() == sortDirection ? " DESC " : " ASC " ;
}
GridView1.Attributes[
" SortExpression " ] = fieldstr;
GridView1.Attributes[
" SortDirection " ] = sortDirection;
BindGrid();
}

protected void GridView1_PageIndexChanging( object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex
= e.NewPageIndex;
BindGrid();
}
}
}

 

 

 

转载于:https://www.cnblogs.com/lsq_NET/archive/2010/07/13/1776250.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值