GridView 实现自定义分页、排序、查询、添加、编辑、多选删除、导出、突出显示等常见应用实现

本人初学,其中参考了无锋不起浪之http://www.cnblogs.com/wf225/archive/2007/08/10/850218.html


不妥之处还望各位指正,谢谢!!

前台代码

BookInfo.aspx

 

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

<! 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 >
< script  language ="javascript"  type ="text/javascript" >
    
    
function $(s){return document.getElementById(s);}
    
    
function isNull(_sVal){return (_sVal == "" || _sVal == null || _sVal == "undefined");}
    
    
//获取地址栏参数
    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.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 CheckAll(spanChk)//全选
    {
        
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.type=="checkbox" && elm.id!=theBox.id)
        
{
            
if(elm.checked!=xState)
            elm.click();
        }

    }

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

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

        
return !isNull(HiddenID);
    }

</ script >
    
< title > 图书信息 </ title >
    
< link  href ="../Main.css"  type ="text/css"  rel ="stylesheet"   />
</ head >
< body >
    
< form  id ="Form1"  runat ="server" >
        
< b > 图书信息增加 </ b > &nbsp;&nbsp;&nbsp;&nbsp;  
        
< asp:Button  class ="input_button"  id ="BtnClear"  Text ="清  空"  runat ="server"  CausesValidation ="False"  OnClick ="BtnClear_Click" ></ asp:Button >
        
&nbsp;&nbsp;&nbsp;&nbsp;  
        
< asp:button  class ="input_button"  id ="BtnSave"   runat ="server"  text ="保  存"  OnClick ="BtnSave_Click" ></ asp:button >
        
&nbsp;&nbsp;&nbsp;&nbsp;
        
< asp:Label  ID ="LblCode1"  Text ="图书编号"  ForeColor ="Red"  runat ="server"  Visible ="false" ></ asp:Label >
        
< asp:Label  ID ="LblCode"  Text =""  ForeColor ="Red"  runat ="server"  Visible ="false" ></ asp:Label >
        
< hr  size ="1"   />
        
< table  class ="table_1"  id ="search"  cellspacing ="1"  cellpadding ="0"  width ="95%"  align ="center" >
            
< tbody >
                
< tr  class ="tr2" >
                    
< td  style ="PADDING-LEFT: 5px"  height ="25" >
                        图书ISBN: 
                        
< asp:TextBox  class ="input_text"  id ="TxtISBN"  runat ="server"  width ="200" ></ asp:TextBox >
                    
</ td >
                    
< td  style ="PADDING-LEFT: 5px"  width ="47%"  height ="25" >
                        出 版 社: 
                        
< asp:DropDownList  class ="input_text"  id ="DdlSelPress"  runat ="server"  width ="200" ></ asp:DropDownList >
                    
</ td >
                
</ tr >
                
< tr  class ="tr1" >
                    
< td  style ="PADDING-LEFT: 5px"  colspan ="2"  height ="25" >
                        图书名称: 
                        
< asp:TextBox  class ="input_text"  id ="TxtName"  runat ="server"  width ="600" ></ asp:TextBox >
                        
< asp:RequiredFieldValidator  id ="Requiredfieldvalidator1"  runat ="server"  ErrorMessage ="*"  ControlToValidate ="TxtName" > 此项必填 </ asp:RequiredFieldValidator >
                    
</ td >
                
</ tr >
                
< tr  class ="tr2" >
                    
< td  style ="PADDING-LEFT: 5px"  height ="25" >
                        定
&nbsp;&nbsp;&nbsp;&nbsp; 价: 
                        
< asp:TextBox  class ="input_text"  id ="TxtBookPrice"  runat ="server"  width ="200" ></ asp:TextBox >
                        
< asp:RequiredFieldValidator  id ="Requiredfieldvalidator2"  runat ="server"  ErrorMessage ="*"  ControlToValidate ="TxtBookPrice" > 此项必填 </ asp:RequiredFieldValidator >
                    
</ td >
                    
< td  style ="PADDING-LEFT: 5px"  width ="47%"  height ="25" >
                        图书源码: 
                        
< asp:TextBox  class ="input_text"  id ="TxtSourceCode"  runat ="server"  width ="200" ></ asp:TextBox >
                    
</ td >
                
</ tr >
                
< tr  class ="tr1" >
                    
< td  style ="PADDING-LEFT: 5px"  height ="25" >
                        自编代码: 
                        
< asp:TextBox  class ="input_text"  id ="TxtBookSelfCode"  runat ="server"  width ="200" ></ asp:TextBox >
                    
</ td >
                    
< td  style ="PADDING-LEFT: 5px"  width ="47%"  height ="25" >
                        出版年度: 
                        
< asp:TextBox  class ="input_text"  id ="TxtCbyYear"  runat ="server"  width ="200" ></ asp:TextBox >
                    
</ td >
                
</ tr >
                
< tr  class ="tr2" >
                    
< td  style ="PADDING-LEFT: 5px"  height ="25" >
                        版
&nbsp;&nbsp;&nbsp;&nbsp; 次: 
                        
< asp:TextBox  class ="input_text"  id ="TxtBc"  runat ="server"  width ="200" ></ asp:TextBox >
                    
</ td >
                    
< td  style ="PADDING-LEFT: 5px"  width ="47%"  height ="25" >
                        作
&nbsp;&nbsp;&nbsp;&nbsp; 者: 
                        
< asp:TextBox  class ="input_text"  id ="TxtBookAuthor"  runat ="server"  width ="200" ></ asp:TextBox >
                    
</ td >
                
</ tr >
                
< tr  class ="tr1" >
                    
< td  style ="PADDING-LEFT: 5px"  height ="25" >
                        架 位 号: 
                        
< asp:TextBox  class ="input_text"  id ="TxtLocateCode"  runat ="server"  width ="200" ></ asp:TextBox >
                    
</ td >
                    
< td  style ="PADDING-LEFT: 5px"  width ="47%"  height ="25" >
                        备
&nbsp;&nbsp;&nbsp;&nbsp; 注: 
                        
< asp:TextBox  class ="input_text"  id ="TxtMemo"  runat ="server"  width ="200" ></ asp:TextBox >
                    
</ td >
                
</ tr >
            
</ tbody >
        
</ table >
        
< br  />
        
< b > 图书信息查询 </ b >  
        
< hr  size ="1"   />
        
< table  class =""  id ="Table1"  cellspacing ="1"  cellpadding ="0"  width ="95%"  align ="center"  border ="0" >
            
< tbody >
                
< tr  class ="tr2" >
                    
< td  style ="PADDING-LEFT: 5px; height: 25px;" >
                        检索条件: 
                        
< asp:DropDownList  class ="input_text"  id ="DdlQuery_tj"  runat ="server"  width ="97" >
                            
< asp:ListItem  Value ="name"  Selected ="True" > 图书名称 </ asp:ListItem >
                            
< asp:ListItem  Value ="ISBN" > 图书ISBN </ asp:ListItem >
                            
< asp:ListItem  Value ="author" > 作者 </ asp:ListItem >
                            
< asp:ListItem  Value ="abbrname" > 出版社名称 </ asp:ListItem >
                            
< asp:ListItem  Value ="memo" > 备注 </ asp:ListItem >
                        
</ asp:DropDownList >
                        
&nbsp;&nbsp; 检索内容: 
                        
< asp:TextBox  class ="input_text"  id ="TxtQuery_content"  runat ="server"  width ="120" ></ asp:TextBox >
                        
&nbsp;&nbsp;  
                        
< asp:button  class ="input_button"  id ="BtnQuery"  runat ="server"  text ="查  询"  CausesValidation ="False"  OnClick ="BtnQuery_Click" ></ asp:button >
                        
< br  />
                        
< b >< asp:Label  id ="LblMsg"  runat ="server"  forecolor ="Red" ></ asp:Label >< br  />
                            
< asp:Button  ID ="BtnDelChecked"  runat ="server"  Height ="25px"  OnClick ="BtnDelChecked_Click"
                                Text
="删除选中"  Width ="100px"  CausesValidation ="false"  Visible ="false"   OnClientClick ="javascript:return ( checkSelect() && confirm('警告:删除将无法恢复!确认删除吗?') );"    />
                            
&nbsp;
                            
< asp:Button  ID ="BtnToExcel"  runat ="server"  CausesValidation ="False"  Height ="25px"
                                OnClick
="BtnToExcel_Click"  Text ="导出到Excel"  Width ="100px"   />
                            
&nbsp;
                            
< asp:Button  ID ="BtnToWord"  runat ="server"  CausesValidation ="False"  Height ="25px"
                                OnClick
="BtnToWord_Click"  Text ="导出到Word"  Width ="100px"   /></ b >
                            
< input  type ="hidden"  id ="HiddenID"  name ="HiddenID"  runat ="server" />
                    
</ td >
                
</ tr >
                
< tr >
                    
< td  style ="PADDING-LEFT: 5px"  height ="25" >
                         
< asp:GridView  ID ="gridViewPublishers"  runat ="server"  CellPadding ="4"
                            ForeColor
="#333333"  GridLines ="None"  AllowPaging ="True"  AllowSorting ="True"   AutoGenerateColumns ="False"  OnPageIndexChanging ="GridViewPublishersPageChanged"  OnSorting ="GridViewPublishersSorting"  OnRowDeleting ="GridViewRowDelete"  DataKeyNames ="code"  OnRowEditing ="GridViewRowEdit"  OnRowDataBound ="gridViewPublishers_RowDataBound" >
                            
< FooterStyle  BackColor ="#1C5E55"  Font-Bold ="True"  ForeColor ="White"   />
                            
< RowStyle  BackColor ="#E3EAEB"   />
                            
< EditRowStyle  BackColor ="#7C6F57"   />
                            
< SelectedRowStyle  BackColor ="#C5BBAF"  Font-Bold ="True"  ForeColor ="#333333"   />
                            
< PagerStyle  BackColor ="#666666"  ForeColor ="White"  HorizontalAlign ="Right"   />
                            
< HeaderStyle  BackColor ="#1C5E55"  Font-Bold ="True"  ForeColor ="White"   />
                            
< AlternatingRowStyle  BackColor ="White"   />
                             
< Columns >
                                 
< asp:TemplateField >
                                    
< ItemTemplate >
                                        
< input  id ="check"  runat ="server"  type ="checkbox"  value ='<%#  Eval("Code") % > ' />
                                    
</ ItemTemplate >
                                    
< HeaderTemplate >
                                        
< input  id ="checkAll"  runat ="server"  type ="checkbox"  onclick ="javascript:CheckAll(this);" />
                                    
</ HeaderTemplate >
                                 
</ asp:TemplateField >
                                 
< asp:BoundField  DataField ="ID"  DataFormatString ="{0:d}"  HeaderText ="序號"   />
                                 
< asp:BoundField  DataField ="code"  HeaderText ="圖書編號"  SortExpression ="code"   />
                                 
< asp:BoundField  DataField ="name"  HeaderText ="圖書名稱 "  SortExpression ="name"   />
                                 
< asp:BoundField  DataField ="abbrname"  HeaderText ="版別"  SortExpression ="abbrname"   />
                                 
< asp:BoundField  DataField ="price"  DataFormatString ="{0:C}"  HtmlEncode ="false"  HeaderText ="定價"  SortExpression ="price"   />
                                 
< asp:BoundField  DataField ="Author"  HeaderText ="作者"  SortExpression ="author"   />
                                 
< asp:BoundField  DataField ="version"  HeaderText ="版次"  SortExpression ="version"   />
                                 
< asp:BoundField  DataField ="storeamountz"  HeaderText ="總店庫存"  SortExpression ="storeamountz"   />
                                 
< asp:BoundField  DataField ="storeamounta"  HeaderText ="門市庫存"  SortExpression ="storeamounta"   />
                                 
< asp:TemplateField >
                                    
< ItemTemplate >
                                        
< asp:Button  ID ="del"  runat ="server"  CausesValidation ="false"  Text ="删除"  CommandName ="Delete"  OnClientClick ="return confirm('您确认删除该记录吗?');"  CommandArgument ='<%#  Eval("Code") % > '> </ asp:Button >
                                    
</ ItemTemplate >
                                    
< HeaderTemplate >
                                        删除
                                    
</ HeaderTemplate >
                                 
</ asp:TemplateField >
                                 
< asp:CommandField  ButtonType ="Button"  CausesValidation ="False"  HeaderText ="編輯"  ShowEditButton ="True"   />
                             
</ Columns >
                        
</ asp:GridView >
                    
</ td >
                
</ tr >
            
</ tbody >
        
</ table >
    
</ form >
</ body >
</ html >

后台代码

BookInfo.aspx.cs

 

using  System;
using  System.Data;
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.Data.SqlClient;

public   partial   class  ZDWH_BookInfo : System.Web.UI.Page
{

    
Page_Load()

    
GetAlertScript(string str)

    
綁定出版社

    
属性

    
方法

    
数据库联接操作

    
GridView操作

    
事件

}

你可能感兴趣的:(GridView 实现自定义分页、排序、查询、添加、编辑、多选删除、导出、突出显示等常见应用实现)