以较少代码实现DataGrid的排序,翻页,删除等功能 【1】

通过ASP.Net2.0中的母版、主题以及面向对象中的继承、多态等技术可以节省很大的代码量。
以下是我实现的DataGrid的排序,翻页,删除等功能。
首先看一下运行后的效果图:

在这个页面中,实现了排序,分页,删除,编辑等功能,用户选择记录,点击“删除”按钮后,可删除记录。用户点击添加,编辑,删除,查看,查询等按钮,会调用相应的脚本,这里就不再描述脚本代码了。
下面是实现代码:
页面代码:
<% @ Page Language = " C# "  CodeFile = " Student_List.aspx.cs "  Inherits = " Tables_Student_List "
    MasterPageFile
= " ~/Master/ListMaster.master "  Title = " 学生--列表 "   styleSheetTheme="Default" %>

< asp:Content ID = " Main "  ContentPlaceHolderID = " DefaultContent "  runat = " server " >

    
< script >     
    
//  操作表名称
    
//  作者:天志
    
//  创建日期:2007-11-26
    function GetTableName()
    {
        
return   " Student " ;
    }
    
    
//  参数字符串
    
//  作者:天志
    
//  创建日期:2007-11-26
    function GetQuery()
    {
        var Table 
=  document.all( " <%=dgdList.ClientID %> " );
        var StudentGUID 
=  getCheckKeyId(Table,  0 1 );

        
return   " StudentGUID= "   +  StudentGUID;
    }
    
</ script >
    
< div align = " Center " >
        
< asp:DataGrid ID = " dgdList "  SkinID = " dgdList "  runat = " server " >
            
< Columns >
                
< asp:TemplateColumn >
                    
< HeaderTemplate >
                        
< input id = " chkselectall "  type = " checkbox "  onclick = " checkallorno(this) "  style = " display: none " >
                    
</ HeaderTemplate >
                    
< ItemTemplate >
                        
< asp:CheckBox id = " chkID "  onclick = " ClickCheckInDataGrid(this) "  runat = " server "   />
                        
< input type = " hidden "  id = " idStudentGUID "  value = ' <%# DataBinder.Eval(Container.DataItem, "StudentGUID", "")%> '  NAME = " idStudentGUID " >

                    
</ ItemTemplate >
                
</ asp:TemplateColumn >
                
< asp:BoundColumn DataField = " StudentGUID "  Visible = " False "   />
                
< asp:BoundColumn DataField = " StudentName "  HeaderText = " 学生名称 "  SortExpression = " StudentName "  ItemStyle - HorizontalAlign = " Left "  HeaderStyle - HorizontalAlign = " Left " />
                
< asp:BoundColumn DataField = " ClassGUID "  HeaderText = " 学生班级 "  SortExpression = " ClassGUID "  ItemStyle - HorizontalAlign = " Left "  HeaderStyle - HorizontalAlign = " Left " />

            
</ Columns >
        
</ asp:DataGrid >
    
</ div >
</ asp:Content >


后台代码:
/**/ ///   <copyright> 天志 1980--2007 </copyright>
///   <version> 1.0 </version>
///   <author> 天志 </author>
///   <email> Xumingxsh21@126.com </email>
///   <log date="2007-11-26"> 创建 </log>

using  System;
using  System.Data;
using  System.Web.UI.WebControls;
using  System.Web.UI.HtmlControls;

/**/ ///   <summary>
///  学生--列表记录。
///   </summary>
///   <author> 天志 </author>
///   <log date="2007-11-26"> 创建 </log>
public  partial  class  Tables_Student_List : BaseListPage
{
    
/**/ ///   <summary>
    
///  装载页面数据。
    
///   </summary>
    
///   <param name="sender"> 页面对象 </param>
    
///   <param name="e"> 存储事件数据对象 </param>
    
///   <author> 天志 </author>
    
///   <log date="2007-11-26"> 创建 </log>
     private   void  Page_Load( object  sender,  System.EventArgs e)
    {
        
try
        {
            ((Master_ListMaster)
this .Master).OnDeleteEvent  +=   new  Master_ListMaster.DeleteHandler(btnDel_Click);
            
            
if  ( ! IsPostBack)
            {
                
//  绑定DataGrid数据源
                 this .dgCommon.BindDataGrid( this .dgdList,   true );
            }
        }
        
catch (Exception ex)
        {
            
this .OnException(ex);
        }
    }


    
/**/ ///   <summary>
    
///  取得数据源。
    
///   </summary>
    
///   <author> 天志 </author>
    
///   <log date="2007-11-26"> 创建 </log>
     protected   override  DataView GetDataView()
    {
        
string  sql  =   " SELECT StudentGUID, StudentName, ClassGUID FROM Student " ;
        
        
return  dataTable.DefaultView;
    }

    
/**/ ///   <summary>
    
///  设置事件的DataGrid。
    
///   </summary>
    
///   <author> 天志 </author>
    
///   <log date="2007-11-26"> 创建 </log>
     protected   override  DataGrid InitialDataGridControl
    {
        
get
        {
            
return   this .dgdList;
        }
    }

    
/**/ ///   <summary>
    
///  删除。
    
///   </summary>
    
///   <author> 天志 </author>
    
///   <log date="2007-11-26"> 创建 </log>
     protected   void  btnDel_Click()
    {
        
        
this .dgCommon.BindDataGrid( this .dgdList,  true );
    }
}
GetDataView()的代码我省略了,它的作用是取得DataGrid的数据源。
btnDel_Click()的代码我省略了,它的作用是删除DataGrid中一条选中的记录。


以上就是实现这些功能的代码,是不是代码很少?
为什么只用这么少的代码就可以实现这些功能呢?
下一篇将介绍:
以较少代码实现DataGrid的排序,翻页,删除等功能(续)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值