DataGrid,DataList,Repeater等数据访问控件的区别及总结、使用Repeater控件显示数据

115 篇文章 2 订阅
 

DataGrid,DataList,Repeater等数据访问控件的区别及总结

一.数据访问控件的区别
1.功能从弱到强依次为Repeater->DataList->DataGrid
2.功能明细
...............Repeater:....................................................................->
 能够绑定显示基本的数据,方便使用,灵活,自主选择性大
 有5种模板: ItemTemplate,HeaderTemplate,FooterTemplate,SeparatorTemplate,AlternatingItemTemplate


...............DataList:.....................................................................->
 除了Repeater的功能之外,还有编辑,查询的功能
 SelectedItemTemplate,EditItemTemplate


 (当一次只显示主题信息,然后点击详细信息时再显示SelectedItemTemplate里的内容,ItemTemplate里要设置 LinkButton,CommandName="select",DataList属性里设置事件响应OnItemCommand="DataList_ItemCommand")
 eg:
 public void DataList_ItemCommand(Object sender,DataListCommandEventArgs e)
 {
 score.SelectedIndex=e.Item.ItemIndex;
 ListBind();
 }


 (当要编辑的时候,项模板也要设置,只是CommandName="edit",再显示EditItemTemplate里的内容,里面有两个 LinkButton,CommandName="edit",CommandName="cancel")
 eg:
<asp:DataList id="score" runat="server" RepeatLayout="Table" OnEditCommand="DataList_EditCommand"
OnUpdateCommand="DataList_UpdateCommand"
OnCancelCommand="DataList_CancelCommand"
>
<HeaderTemplate>演示DataList控件的编辑功能</HeaderTemplate>
 
 <ItemTemplate>
 姓名:<%# DataBinder.Eval(Container.DataItem,"Name")%>
 <asp:LinkButton id="btnselect" Text="编辑" CommandName="edit" runat="server"/>
 </ItemTemplate>

 <EditItemTemplate>
 姓名:<asp:Label id="lbName" Text='<%# DataBinder.Eval (Container.DataItem,"Name")%>'   runat="server"/><br>
 语文成绩:<asp:TestBox id="tbChinese" Text='<% DataBinder.Eval  (Container.DataItem,"Chinese")%>' runat="server"/><br>
 数学成绩:<asp:TestBox id="tbMath" Text='<% DataBinder.Eval    (Container.DataItem,"Math")%>' runat="server"/><br>
 <asp:LinkButton id="lbnUpdate" Text="更新" CommandName="update" runat="server"/>
 <asp:LinkButton id="lbnCancel" Text="取消" CommandName="cancel" ruant="server"/>
 </EditItemTemplate>

</asp:DataList>

 eg:
 public void DataList_EditCommand(Object sender,DataListCommandEventArgs e)
{
 score.EditItemIndex=(int)e.Item.ItemIndex;
 ListBind();
}
 public void DataList_CancelCommand(Object sender,DataListCommandEventArgs e)
{
 score.EditItemIndex=-1;(默认为-1时为初始状态)
 ListBind();
}
public void DataList_UpdateCommand(Object sender,DataListCommandEventArgs e)
{
 int intChinese=Int32.Parse(((TextBox)e.Item.FindControl("tbChinese")).Text);
 ..........................
 ..........................
 string strUpdate="update Score set Chinese='"+intChinese+"',...,...,
 执行修改更新即可
 
 score.EditItemIndex=-1;
 ListBind();
}


 DataList属性里有RepeatLayout默认为Table,也可以设置为Flow则和Repeater一样
 DataList属性里有RepeatDirection和RepeatColumns可以在不严格定义表格的情况下显示布局

....................DataGrid......................................................................->
有BoundColumn,HyperLinkColumn,ButtonColumn(事件为OnItemCommand,ButtonType属性可设置为pushButton成为标准Button按钮),EditCommandColumn,TemplateColumn(下面有ItemTemplate,HeaderTemplate,FooterTemplate等等)
有分页,排序,编辑,查询功能等
EditCommandColumn属性里设置EditText,CancelText,UpdateText,ButtonType等属性,
然后绑定列,DataGrid里设置响应事件OnEditCommand,OnCancelCommand,OnUpdateCommand,编写事件响应函数代码
(Object sender,DataGridCommandEventArgs e)

分页功能:
DataGrid属性里设置id="",AllowPaging="true" OnPageIndexChanged事件响应,PageSize=""(每页记录数),PagerStyle-下一页:Ajax联动下拉框的实现例子 Text="",PagerStyle-PrevPageText="",PagerStyle-HorizontalAlign="",PageStyle-Mode="NumericPages"(显示123数字),AllowCustomPaging="true"
public void PageChanged(Object sender,DataGridPageChangedEventArgs e)
{
 usrGrid.CurrentPageIndex=e.NewPageIndex;
 BindGrid();
}
ICollection CreateTable()
{
 .....................
 .....................
 return ds.Tables["Score"].DefaultView;
}
public void BindGrid()
{
 DataView dv=(DataView)CreateTable();
 usrGrid.VirtualItemCount=dv.Count;(系统利用VirtualItemCount属性和PageSize的值来分页)
 usrGrid.DataSource=dv;
 usrGrid.DataBind();
}/*******利用了视图的(dv.Count)Count属性
排序功能:
DataGrid属性里设置AllowSorting="true',OnSortCommand响应事件函数
eg:
public void DataGrid_Sort(Object sender,DataGridSortCommandEventArgs e)
{
 ViewState["SortField"]=(string)e.SortExpression;
 BindGrid();
}
public void BindGrid()
{
 DataView dv=(DataView)CreateTable();
 dv.Sort=(string)ViewState["SortField"];
 score.DataSource=dv;
 score.DataBind();
}//*******利用了视图的(dv.Sort)Sort属性
注:绑定列中必须设置SortExpression属性

删除功能:
可以通过e.Item.ItemIndex获得索引项,然后新建一行,DataRow dr=ds.Tables[tablename].Rows[(int)e.Item.ItemIndex]
dr.Delete();
adr.Update(ds,tablename);
但这样会有一个不好处,就是如果有DataGrid排序功能,则不能根据索引来删除,可以绑定一个id,然后根据e.Item.Cell[0].Controls[0],获得 id,然后据id删除,或者e.Item.FindControl["id"];
得到id号

编辑功能:
和DataList一样,略.........................

 

>>更多“DataGrid,DataList,Repeater等数据访问控件的区别及总结”阅读推荐


使用Repeater控件显示数据[C#,ASP.NET]


如果你正在使用ASP.NET,你一定对DataGrid控件非常熟悉。DataGrid控件提供了各种特性,通过这些特性可以很容易地在一个Web页面上以列表形式显示数据。但是,如果你不想使用HTML表格形式呢?此时,可以使用一个DataGrid的一个鲜为人知的兄弟控件,即Repeater控件。Repeater控件提供显示你所需要数据的灵活性。

Repeater控件是什么?
 
Repeater是一个可重复操作的控件,也就是说,它通过使用模板显示一个数据源的内容,而你可以很容易地配置这些模板。Repeater包含如标题和页脚这样的数据,它可以遍历所有的数据选项并应用到模板中。

与DataGrid和DataList控件不同,Repeater控件并不是由WebControl类派生而来。所以,它不包括一些通用的格式属性,比如控制字体,颜色,等等。然而,使用Repeater控件,HTML(或者一个样式表)或者ASP.NET类可以处理这些属性。

HTML在哪里?
Repeater控件与DataGrid (以及DataList)控件的主要区别是在于如何处理HTML。ASP.NET建立HTML代码以显示DataGrid控件,但Repeater允许开发人员决定如何显示数据。所以,你可以选择将数据显示在一个HTML表格中或者一个顺序列表中。这主要取决于你的选择,但你必须将正确的HTML插入到ASP.NET页面中。

模板与DataList一样,Repeater控件只支持模板。以下的模板可供选择:

AlternatingItemTemplate: 指定如何显示每一其它选项。 
ItemTemplate: 指定如何显示选项。(AlternatingItemTemplate可以覆盖这一模板。) 
HeaderTemplate: 建立如何显示标题。 
FooterTemplate: 建立如何显示页脚。 
SeparatorTemplate: 指定如何显示不同选项之间的分隔符。 
你可以使用这些模板来显示你希望的数据。唯一具有强制性的模板是ItemTemplate,所有其它的模板都是具有选择性的。

数据
对于处理一个数据源,Repeater控件具有与DataGrid与DataList相同的属性:

DataMember:获得或者设置与 Repeater 控件绑定的相应DataSource属性的表格。

DataSource:获得或者设置为 Repeater 显示提供数据的数据源。

除此之外,还有一个Items属性,你可以通过这一属性编程访问Repeater数据中单一选项。它返回一个RepeaterItemCollection对象,为一组RepeaterItem对象的集合,代表 Repeater 数据的每一行。

ASP.NET Web数据控件还有其它一个共性:它们都使用DataBind方法来生成用户界面。调用这一方法可以返回并显示数据(假设DataSource和DataMember属性设置正确)。在查看DataBind方法之前,我们先看看如何在一个Web页面中使用一个Repeater控件。

使用Repeater控件
使用Repeater控件的第一步骤是决定我们将要使用的数据源和字段。例如,我们将要使用SQL Server Northwind数据库中的Employees列表。Web页面将显示职工的完整名字,地址,以及电话号码。HTML将使用DIV标记,用 Repeater 模板来分隔内容。下面是 Web 页面的 HTML 内容:

<%@ Page language="c#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html><head>
<title>Builder.com Repeater Example</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<style>
.alternate {
FONT-WEIGHT: bold;
COLOR: black;
FONT-FAMILY: Verdana, 'Times New Roman';
BACKGROUND-COLOR: yellow
}
.row {
FONT-WEIGHT: bold;
COLOR: black;
FONT-FAMILY: Verdana, 'Times New Roman';
BACKGROUND-COLOR: white
}
.footer {
FONT-WEIGHT: bold;
COLOR: red;
FONT-FAMILY: Verdana, 'Times New Roman';
BACKGROUND-COLOR: gray
}
.header {
FONT-WEIGHT: bold;
COLOR: yellow;
FONT-FAMILY: Verdana, 'Times New Roman';
BACKGROUND-COLOR: gray
}
.box {
BORDER-RIGHT: blue groove;
BORDER-TOP: blue groove;
DISPLAY: block;
VERTICAL-ALIGN: baseline;
OVERFLOW: auto;
BORDER-LEFT: blue groove;
CURSOR: wait;
BORDER-BOTTOM: blue groove;
FONT-FAMILY: verdana;
TEXT-ALIGN: center
}
body {
background: #333;
}
</style>
<script language="C#" runat="server">
private void Page_Load(object sender, System.EventArgs e) {
if (!IsPostBack) {
DataSet dset = new DataSet();
string conn = "server=(local);Initial Catalog=Northwind;UID=ctester;PWD=password";
string qry = "SELECT firstname, lastname, address, city, region, postalcode,
homephone FROM employees";
SqlDataAdapter sda = new SqlDataAdapter(qry, conn);
sda.Fill(dset);
Repeater1.DataSource = dset.Tables[0].DefaultView;
Repeater1.DataBind();
} }
</script></head>
<body MS_POSITIONING="GridLayout" bgColor="#00cc99">
<form id="Form1" method="post" runat="server">
<div class="box">
<asp:Repeater id="Repeater1" runat="server">
<HeaderTemplate>
<div class="header" id="header">Northwind Employees</div>
</HeaderTemplate>
<SeparatorTemplate><hr /></SeparatorTemplate>
<ItemTemplate><div class="row">
<%# ((DataRowView)Container.DataItem)["FirstName"] %>
<%# ((DataRowView)Container.DataItem)["LastName"] %><br>
<%# ((DataRowView)Container.DataItem)["Address"] %><br>
<%# ((DataRowView)Container.DataItem)["City"] %>,  
<%# ((DataRowView)Container.DataItem)["Region"] %>   
<%# ((DataRowView)Container.DataItem)["PostalCode"] %><br>
<%# ((DataRowView)Container.DataItem)["HomePhone"] %>
</div></ItemTemplate>
<AlternatingItemTemplate><div class="alternate">
<%# ((DataRowView)Container.DataItem)["FirstName"] %>
<%# ((DataRowView)Container.DataItem)["LastName"] %><br>
<%# ((DataRowView)Container.DataItem)["Address"] %><br>
<%# ((DataRowView)Container.DataItem)["City"] %>,  
<%# ((DataRowView)Container.DataItem)["Region"] %>   
<%# ((DataRowView)Container.DataItem)["PostalCode"] %><br>
<%# ((DataRowView)Container.DataItem)["HomePhone"] %>
</div></AlternatingItemTemplate>
<FooterTemplate><div class="footer">
<%# ((DataView)Repeater1.DataSource).Count + " employees found." %>
</div></FooterTemplate>
</asp:Repeater></div></form></body></html>
 

可以注意到,每个 Repeater 行中样式表控制着文字的外观。除此之外,在网页内容中还添加了一个文本框。嵌入式 C# 代码从 Repeater 的数据源中获取相应的列。每个数据项都被转换成一个DataRowView对象以便显示。

这一页面并不是使用ASP.NET的“后台代码”性质。由于这一原因,页面引用了两个System.Data和System.Data.SqlClient空间名称。这对于使用DataRowView对象和与 SQL Server 交互访问是必需的。

当调用页面时就会触发Page_Load事件。此时会把数据源连接到 Repeater 控件上,并查询数据库。每一Repeater行的代码从潜在数据源中载入数据,并且Web页面显示这些数据。

这一设计使用了样式表(以及 HTML div 标记),所以更改外观只需要更改必要的样式表代码。为了进一步地将数据和显示分离,你可以在一个独立文件中存储样式表,并以一个HTML LINK标记来引用它们。

一个很好的选择
当与其他 ASP.NET 开发者交流时,开发人员对 Repeater 控件知之甚少,这真让我感到惊讶。虽然它不如DataGrid功能强大,但它在许多场合中仍然提供了出色的灵活性。Repeater 控件缺少编辑和排序功能,但可以通过进一步编程而实现
友情合作伙伴:第一网络!本文出自:http://w1.org.cn/web/programming/dataview.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值