GridView 实现自定义分页、排序、查询、添加、编辑、多选删除

运行效果图:


PM_Base.aspx
<% @ Page Language="C#" AutoEventWireup="true" CodeFile="PM_Base.aspx.cs" Inherits="PM_Base"
    EnableEventValidation
="false" 
%>

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html  xmlns ="http://www.w3.org/1999/xhtml" >
< head  id ="Head1"  runat ="server" >
    
< title > 基本信息 </ title >
</ head >
< body >
    
< form  id ="form1"  runat ="server" >
        
<!-- 导航事件按钮 -->
        
< table  cellpadding ="0"  cellspacing ="0"  width ="%" >
            
< tr >
                
< td  align ="right"  style ="height: 25px;"  nowrap ="nowrap" >
                    
< input  type ="hidden"  id ="HiddenID"  name ="HiddenID"  runat ="server"   />
                    
< input  id ="btnView"  runat ="server"  type ="button"  value ="查看"  style ="width: 50px; height: 20px;"
                        class
="ClassBtn"  onclick ="return View();"   />
                    
< asp:Button  ID ="btnSearch"  runat ="server"  Text ="查询"  Enabled ="false"  Width ="50px"
                        Height
="20px"  OnClick ="btnSearch_Click"   />
                    
< input  id ="btnAdd"  runat ="server"  type ="button"  value ="添加"  style ="width: 50px; height: 20px;"
                        class
="ClassBtn"  onclick ="return Add();"   />
                    
< input  id ="btnEdit"  runat ="server"  type ="button"  value ="修改"  style ="width: 50px; height: 20px;"
                        class
="ClassBtn"  onclick ="return Edit();"   />
                    
< asp:Button  ID ="btnDelete"  runat ="server"  Text ="删除"  Width ="50px"  Height ="20px"  OnClick ="btnDelete_Click"
                        OnClientClick
="javascript:return ( checkSelect() && confirm('警告:删除将无法恢复!\n确认删除吗?') );"   />
                    
< asp:Button  ID ="btnPrint"  runat ="server"  Text ="打印"  Width ="50px"  Height ="20px"  OnClick ="btnToExcel_Click"   />
                    
< asp:Button  ID ="btnOut"  runat ="server"  Text ="导出"  Width ="50px"  Height ="20px"  OnClick ="btnToExcel_Click"   />
                
</ td >
            
</ tr >
        
</ table >
        
< asp:GridView  ID ="GridView1"  SkinID ="gvClass2"  runat ="server"  Width ="%"  DataKeyNames ="ID"
            AutoGenerateColumns
="False"  OnRowDataBound ="GridView1_RowDataBound"  OnDataBound ="GridView1_DataBound"
            AllowSorting
="True"  OnSorting ="GridView1_Sorting"  AllowPaging ="True"  PageSize =""
            OnPageIndexChanging
="GridView1_PageIndexChanging" >
            
< PagerTemplate >
                
< table  width ="%" >
                    
< tr >
                        
< td  width ="%"  align ="left" >
                            
< asp:Label  ID ="MessageLabel"  ForeColor ="Blue"  Text ="页码:"  runat ="server"   />
                            
< asp:DropDownList  ID ="PageDropDownList"  AutoPostBack ="true"  OnSelectedIndexChanged ="PageDropDownList_SelectedIndexChanged"
                                runat
="server"   />
                            
< asp:LinkButton  CommandName ="Page"  CommandArgument ="First"  ID ="linkBtnFirst"  runat ="server" > 首页 </ asp:LinkButton >
                            
< asp:LinkButton  CommandName ="Page"  CommandArgument ="Prev"  ID ="linkBtnPrev"  runat ="server" > 上一页 </ asp:LinkButton >
                            
< asp:LinkButton  CommandName ="Page"  CommandArgument ="Next"  ID ="linkBtnNext"  runat ="server" > 下一页 </ asp:LinkButton >
                            
< asp:LinkButton  CommandName ="Page"  CommandArgument ="Last"  ID ="linkBtnLast"  runat ="server" > 尾页 </ asp:LinkButton >
                        
</ td >
                        
< td  width ="%"  align ="right" >
                            
< asp:Label  ID ="CurrentPageLabel"  ForeColor ="Blue"  runat ="server"   />
                        
</ td >
                    
</ tr >
                
</ table >
            
</ PagerTemplate >
            
< Columns >
                
< asp:TemplateField >
                    
< ItemTemplate >
                        
< input  id ="check"  runat ="server"  type ="checkbox"  value ='<%#  Eval("ID") % > ' />
                    
</ ItemTemplate >
                    
< HeaderTemplate >
                        
< input  id ="checkAll"  title ="全选"  onclick ="javascript:CheckAll(this);"  runat ="server"
                            type
="checkbox"   />
                    
</ HeaderTemplate >
                    
< ItemStyle  Width ="5%"   />
                
</ asp:TemplateField >
                
< asp:BoundField  DataField ="Name"  HeaderText ="名称"  SortExpression ="Name" >
                    
< ItemStyle  Width ="%"  HorizontalAlign ="Left"   />
                
</ asp:BoundField >
                
< asp:BoundField  DataField ="Description"  HeaderText ="描述"  SortExpression ="Description" >
                    
< ItemStyle  HorizontalAlign ="Left"   />
                
</ asp:BoundField >
            
</ Columns >
        
</ asp:GridView >
        
< asp:Label  ID ="Message"  runat ="server"  ForeColor ="Red" ></ asp:Label >
    
</ form >
</ body >
</ html >

< script  language ="javascript" >
  
function $(s){return document.getElementById(s);}
  
function isNull(_sVal){return (_sVal == "" || _sVal == null || _sVal == "#ff0000");}
 
//获取地址栏参数
  function GetURL(name)
 
{
     
var URLParams = new Array();
     
var aParams = document.location.search.substr(1).split('&');
     
for (i=0; i < aParams.length; i++)
     
{
         
var aParam = aParams[i].split('=');
         URLParams[aParam[
0]] = aParam[1];
     }

     
//取得传过来的name参数
     return URLParams[name];
 }

 
  
var tgs;
  
var tmp_background_val; 
  
function tog(n,flags)
 

     
if (tgs){
      tgs.style.background
= tmp_background_val ; 
    }
 
    n.style.background
= '#99ccff' ;
    tmp_background_val
=flags;
    tgs
=n;
}


 
function GetRowIndex(obj)
{
    $(
'<%=HiddenID.ClientID %>').value = obj;
}


 
var HiddenID;
 
function checkSelect()
{
    HiddenID
=$('<%=HiddenID.ClientID %>').value;
    
if(isNull(HiddenID))
    
{
        alert(
"请选择一条记录!");
    }

    
return !isNull(HiddenID);
}


 
function CheckAll(spanChk)//CheckBox全选
{
    
var oItem = spanChk.children;
    
var theBox=(spanChk.type=="checkbox")?spanChk:spanChk.children.item[0];
    xState
=theBox.checked;
    elm
=theBox.form.elements;
    
for(i=0;i<elm.length;i++)
    
if(elm[i].type=="checkbox" && elm[i].id!=theBox.id)
    
{
        
if(elm[i].checked!=xState)
        elm[i].click();
    }

}

//---------------------------------------------------------
//
查看
 function View()
{
    
if(checkSelect())
    
{
        
var url="PM_BaseEdit.aspx?ID=" + HiddenID + "&tableName=" + GetURL("tableName");
        winOpen(url);
    }

}

//添加
 function Add()
{
    
var url="PM_BaseAdd.aspx?tableName=" + GetURL("tableName");
    winOpen(url);
}

//编辑
 function Edit()
{
    
if(checkSelect())
    
{
        
var url="PM_BaseEdit.aspx?ID=" + HiddenID + "&tableName=" + GetURL("tableName");
        winOpen(url);
    }

}


//弹出打开新页面-------------------------------------------
 function winOpen(url)
{
    
var sFeatures='height=, width=, top=, left=,toolbar=0, menubar=0, scrollbars=auto, resizable=1, location=0, status=0';
    window.open(url,
"PM_Base",sFeatures);
}


 
function showDialog(url)
{
    
var sFeatures = "dialogHeight:400px;dialogWidth:500px;resizeable:no;help:no;status:no";
    
var obj=showModalDialog(url,window,sFeatures); 
}

</ script >

PM_Base.aspx.cs

using  System;
using  System.Data;
using  System.Data.OleDb;
using  System.Data.SqlClient;
using  System.Configuration;
using  System.Collections;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;
using  System.Web.Caching;
using  Framework.Components;

public   partial   class  PM_Base : Framework.UI.PageBase
{
    
Page事件

    
GridView

    
按钮事件
}

你可能感兴趣的:(GridView)