ASP.NET 2.0中DataList控件与GridView控件的使用实例

  在所有的管理系统中,数据的处理永远都是终点所在,而数据的表现则是用户最终想要得到的可见的结果。数据的表现有很多办法,在ASP.NET中有那么多的控件可以来实现这一目标,服务器控件很多都可以绑定数据源,能够快捷的开发出满意的视图。而我们最长用到的有这么两个,DataList与GridView。DataList更多的变现在数据的列表形式,格式比较自由一些;GridView就是以表格的形式来表现,数据显示会更加规范整洁。
  下面的例子分别给出了两个控件的数据绑定实现例子:
1:DataList在绑定数据之后的显示处理方法:
< asp:DataList ID = " Photos "  runat = " server "  RepeatColumns  =   " 1 "  RepeatLayout = " Table "  GridLines = " Both "  Width = " 100% " >
    
< HeaderTemplate >
    
< table width = " 100% "  cellpadding = " 3 "  cellspacing = " 3 " >
    
< tr >< td width = " 20% " >
    身份
    
</ td >
    
< td >
    圈子主页
    
</ td >
    
< td width = " 20% " >
    创建者
    
</ td >
    
< td width = " 20% " >
    成员数量
    
</ td ></ tr >
    
</ table >
    
</ HeaderTemplate >
    
< ItemTemplate >
    
< table width = " 100% "  cellpadding = " 3 "  cellspacing = " 3 " >
    
< tr >< td width = " 20% " >
    
<% # GroupCodeType.getUserType(( short )Eval( " UserType " )) %>
    
</ td >
    
< td >
    
< a href = " GroupHome.aspx?GroupID=<%# Eval( " GroupID " ) %> "  target = " _blank " ><% # Eval( " GroupName " ) %></ a >
    
</ td >
    
< td width = " 20% " >
    
< a href = " default.aspx?blog=<%# Eval( " UserName " ) %> "  target = " _blank " ><% # Eval( " DisplayName " %></ a >
    
</ td >
    
< td width = " 20% " >
    
<% # Eval( " UserNum " %>
    
</ td ></ tr >
    
</ table >
   
    
</ ItemTemplate >

 2:GridView在绑定数据之后的显示处理方法:

 

< asp:GridView ID = " DiscussionGrid "  runat = " server "  CellPadding = " 4 "  ForeColor = " #333333 "
              GridLines
= " None "  AutoGenerateColumns = " False "  Width = " 100% " >
              
< FooterStyle BackColor = " #5D7B9D "  Font - Bold = " True "  ForeColor = " White "   />
              
< RowStyle BackColor = " #F7F6F3 "  ForeColor = " #333333 "   />
              
< EditRowStyle BackColor = " #999999 "   />
              
< SelectedRowStyle BackColor = " #E2DED6 "  Font - Bold = " True "  ForeColor = " #333333 "   />
              
< PagerStyle BackColor = " #284775 "  ForeColor = " White "  HorizontalAlign = " Center "   />
              
< HeaderStyle BackColor = " #5D7B9D "  Font - Bold = " True "  ForeColor = " White "   />
              
< AlternatingRowStyle BackColor = " White "  ForeColor = " #284775 "   />
              
< Columns >
                
< asp:HyperLinkField DataTextField = " Title "  HeaderText = " 主题 "  DataNavigateUrlFormatString = " GroupDiscussion.aspx?GroupID={0}&amp;DiscussionID={1} "
                  DataNavigateUrlFields
= " GroupID,id "  Target = " _blank "   />
                
< asp:HyperLinkField DataTextField = " DisplayName "  HeaderText = " 发表人 "  DataNavigateUrlFormatString = " Default.aspx?blog={0} "
                  DataNavigateUrlFields
= " UserName "  Target = " _blank "   />
                
< asp:TemplateField FooterText = " dasdfasdf "  HeaderText = " 点击/回复 " >
                  
< HeaderTemplate >
                    点击
/ 回复
                  
</ HeaderTemplate >
                  
< ItemTemplate >
                    
<% # Eval( " VisitNum " %>
                    
/
                    
<% # Eval( " FeedbackNum " %>
                  
</ ItemTemplate >
                
</ asp:TemplateField >
                
< asp:TemplateField FooterText = " dasdfasdf "  HeaderText = " 发布时间 " >
                  
< HeaderTemplate >
                    发布时间
                  
</ HeaderTemplate >
                  
< ItemTemplate >
                    
<% # Eval( " PublishDate " %>
                  
</ ItemTemplate >
                
</ asp:TemplateField >
              
</ Columns >
            
</ asp:GridView >
 服务器控件在显示数据的时候的确提供了强大的支持,不仅是显示数据的支持,还有对数据处理支持,“修改”、“删除”还有“增加”都可以实现,而且方法都很简单。就是因为简单,就是因为对开发人员的简化,使得很多的功能要在服务器控件上来实现会变得很复杂,甚至无法完成。一个客户端代码无法加上去,一个定制的表格边框、样式无法显示出来,因为服务器控件自动生成的代码在位置上摆放的不合适和造成所有布局错位……。

  知道微软是怎么来实现服务器控件的,我们自己也可以来实现它,虽然会比较烦琐,会比较麻烦,但是因为有了不同的需要,我们就不得不自己来做,自己来处理。合理的利用已有的控件,同时继续开发适合自己的个性化控件!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值