`
luliangok
  • 浏览: 775973 次
文章分类
社区版块
存档分类
最新评论

DataBind包括三大方法,Repeater,DataList和DataGrid,这些控件都位于 System.Web.UI.WebControls 命名空间中,从 WebControl 基类中直接或间接派生出来的。这些方法都是通过HTML来显示数据的内

 
阅读更多

DataBind包括三大方法,Repeater,DataList和DataGrid,这些控件都位于 System.Web.UI.WebControls 命名空间中,从 WebControl 基类中直接或间接派生出来的。这些方法都是通过HTML来显示数据的内容。

一、DataList
Repeater 控件是通用的迭代程序,而 DataList 控件则提供专门用于控制列表布局的附加功能。与 Repeater 不同,DataList 呈现其模
板定义元素周围的表行和单元格,从而提供了更为丰富的布局和格式设置功能。例如,DataList 支持 RepeatColumns 和 RepeatDirection 属性,这两项属性分别指定列数和数据项呈现方向(垂直或水平)。DataList 还支持样式特性,如字体大小和字体名称。以下示例分别用了vb.net和c#两种语言来显示如何访问一个包含书名及其他信息的 SQL 数据库,并使用 DataList 控件来显示相关数据。

[VB.net]
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
Dim myConnection As SqlConnection
Dim myCommand As SqlDataAdapter
' Create a connection to the "pubs" SQL database located on the
' local computer.
myConnection = New SqlConnection("server=localhost;" _
& "database=pubs;Trusted_Connection=Yes")
' Connect to the SQL database using a SQL SELECT query to get all
' the data from the "Titles" table.
myCommand = New SqlDataAdapter("SELECT * FROM Titles", myConnection)
' Create and fill a DataSet.
Dim ds As DataSet = new DataSet()
myCommand.Fill(ds)
' Bind MyDataList to the DataSet. MyDataList is the ID for
' the DataList control in the HTML section of the page.
MyDataList.DataSource = ds
MyDataList.DataBind()
End Sub
</script>

<%-- Display the data. -%>
<body>
<%-- Open the DataList control and set it for two columns, to be
filled in horizontal order. --%>
<ASP:DataList id="MyDataList" RepeatColumns="2"
RepeatDirection="Horizontal" runat="server">
<ItemTemplate>
<div style="padding:15,15,15,15;font-size:10pt;font-family:Verdana">
<div style="font:12pt verdana;color:darkred">
<i><b><%# DataBinder.Eval(Container.DataItem, "title")%>
</i></b>
</div>
<br>
<b>Title ID: </b>
<%# DataBinder.Eval(Container.DataItem, "title_id") %><br>
<b>Category: </b>
<%# DataBinder.Eval(Container.DataItem, "type")%><br>
<b>Publisher ID: </b>
<%# DataBinder.Eval(Container.DataItem, "pub_id") %><br>
<b>Price: </b>
<%# DataBinder.Eval(Container.DataItem, "price", "{0:c}") %>
<p>
</div>
</ItemTemplate>
</ASP:DataList>
</body>
</html>


[C#]
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
// Create a connection to the "pubs" SQL database located on the
// local computer.
SqlConnection myConnection = new SqlConnection("server=localhost;" +
"database=pubs;Trusted_Connection=Yes");
// Connect to the SQL database using a SQL SELECT query to get all
// the data from the "Titles" table.
SqlDataAdapter myCommand = new SqlDataAdapter("SELECT * " +
" from Titles", myConnection);
// Create and fill a DataSet.
DataSet ds = new DataSet();
myCommand.Fill(ds);
// Bind MyDataList to the DataSet. MyDataList is the ID for
// the DataList control in the HTML section of the page.
MyDataList.DataSource = ds;
MyDataList.DataBind();
}
</script>

<%-- Display the data. -%>
<body>
<%-- Open the DataList control and set it for two columns, to be
filled in horizontal order. --%>
<ASP:DataList id="MyDataList" RepeatColumns="2"
RepeatDirection= "Horizontal" runat="server">
<%-- Create a DataList control template named "ItemTemplate". --%>
<ItemTemplate>
<div style="padding:15,15,15,15;font-size:10pt;
font-family:Verdana">
<div style="font:12pt verdana;color:darkred">
<i><b><%# DataBinder.Eval(Container.DataItem, "title")%>
</i></b>
</div>
<br>
<b>Title ID: </b>
<%# DataBinder.Eval(Container.DataItem, "title_id") %><br>
<b>Category: </b>
<%# DataBinder.Eval(Container.DataItem, "type")%><br>
<b>Publisher ID: </b>
<%# DataBinder.Eval(Container.DataItem, "pub_id") %><br>
<b>Price: </b>
<%# DataBinder.Eval(Container.DataItem,"price", "{0:c}") %>
<p>
</div>
</ItemTemplate>
</ASP:DataList>
</body>
</html>

二、Repeater

Repeater 控件是一种数据绑定列表控件,其外观完全由其模板来控制。与 DataList 不同,Repeater 控件不在 HTML 表中呈现其模板,并且不具有对选择或编辑的内置支持。以下代码示例显示一个绑定到 SqlDataReader 的 Repeater 控件,该控件返回从一个 SQL 查询中返回的一组只读、只进数据记录,该 SQL 查询包含有关一套书籍的信息。SqlDataReader 在该示例中用于实现最高性能。该示例还定义一个 HeaderTemplate 和一个 FooterTemplate,它们分别在列表的开头和结尾呈现。Repeater 控件为 DataSource 集合中的每一项呈现一次 ItemTemplate,从而迭代绑定数据。它只呈现其模板中包含的元素。

[VB.net]
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
' Create a connection to the "pubs" SQL database located
' on the local computer.
Dim myConnection As SqlConnection
Dim myCommand As SqlDataAdapter
' Connect to the SQL database using a SQL SELECT query to get
' all the data from the "Titles" table.
myConnection = New SqlConnection("server=localhost;" _
& "database=pubs;Trusted_Connection=Yes")
myCommand = New SqlDataAdapter("SELECT * FROM Titles", _
myConnection)
' Create and fill a DataSet.
Dim ds As Dataset = new DataSet()
myCommand.Fill(ds)
' Bind MyRepeater to the DataSet. MyRepeater is the ID of the
' Repeater control in the HTML section of the page.
MyRepeater.DataSource = ds
MyRepeater.DataBind()
End SUb
</script>

<body>
<ASP:Repeater id="MyRepeater" runat="server">
<HeaderTemplate>
<Table width="100%" style="font: 8pt verdana">
<tr style="background-color:DFA894">
<th>
Title
</th>
<th>
Title ID
</th>
<th>
Type
</th>
<th>
Publisher ID
</th>
<th>
Price
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr style="background-color:FFECD8">
<td>
<%# DataBinder.Eval(Container.DataItem, "title") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "title_id") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "type") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "pub_id") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "price", _
"{0:c}") %>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</ASP:Repeater>
</body>
</html>

[C#]
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
// Create a connection to the "pubs" database located
// on the local computer.
SqlConnection myConnection = new SqlConnection("server=localhost;" +
"database=pubs;Trusted_Connection=Yes");
// Connect to the SQL database using a SQL SELECT query to get
// all the data from the "Titles" table.
SqlDataAdapter myCommand = new SqlDataAdapter("SELECT * FROM" +
" Titles", myConnection);
// Create and fill a DataSet.
DataSet ds = new DataSet();
myCommand.Fill(ds);
// Bind MyRepeater to the DataSet. MyRepeater is the ID of the
// Repeater control in the HTML section of the page.
MyRepeater.DataSource = ds;
MyRepeater.DataBind();
}
</script>

<%-- Display the data in the body of the page. --%>
<body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">
<ASP:Repeater id="MyRepeater" runat="server">
<HeaderTemplate>
<Table width="100%" style="font: 8pt verdana">
<tr style="background-color:DFA894">
<th>
Title
</th>
<th>
Title ID
</th>
<th>
Type
</th>
<th>
Publisher ID
</th>
<th>
Price
</th>
</tr>
</HeaderTemplate>

<ItemTemplate>
<tr style="background-color:FFECD8">
<td>
<%# DataBinder.Eval(Container.DataItem, "title") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem,"title_id") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "type") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "pub_id") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem,
"price", "{0:c}") %>
</td>
</tr>
</ItemTemplate>

<FooterTemplate>
</Table>
</FooterTemplate>
</ASP:Repeater>
</body>
</html>

三、DataGrid

多功能的 DataGrid 服务器控件显示表格数据并支持对数据进行选择、排序和编辑。默认情况下,DataGrid 将为数据源中的每个字段生成一个绑定列 (AutoGenerateColumns=true)。每个数据字段都按照它们在数据库的存储顺序显示在单独的列中。以下示例显示作者姓名、地址、电话号码以及其他数据的列表。

[Visual Basic]
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="VB" runat="server">
Sub Page_Load(Src As Object, e As EventArgs)
Dim myConnection As SqlConnection
Dim myCommand As SqlDataAdapter
' Create a connection to the "pubs" SQL database located on the
' local computer.
myConnection = New SqlConnection("server=localhost;" _
& "database=pubs;Trusted_Connection=Yes")
' Connect to the SQL database using a SQL SELECT query to get all
' the data from the "Authors" table.
myCommand = new SqlDataAdapter("SELECT * FROM Authors", _
myConnection)
' Create and fill a DataSet.
Dim ds As DataSet = new DataSet()
myCommand.Fill(ds)
' Bind MyDataGrid to the DataSet. MyDataGrid is the
' ID for the DataGrid control in the HTML section.
MyDataGrid.DataSource = ds
MyDataGrid.DataBind()
End Sub
</script>

<body>
<h3><font face="Verdana">
Simple Select to a DataGrid Control.
</font></h3>
<ASP:DataGrid id="MyDataGrid" runat="server"
Width="700"
BackColor="#ccccff"
BorderColor="black"
ShowFooter="false"
CellPadding=3
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
EnableViewState="false"
/>
</body>
</html>

[C#]
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="C#" runat="server">
protected void Page_Load(Object Src, EventArgs E)
{
// Create a connection to the "pubs" SQL database located on the
// local computer.
SqlConnection myConnection = new SqlConnection("server=localhost;" +
"database=pubs;Trusted_Connection=Yes");
// Connect to the SQL database using a SQL SELECT query to get all
// the data from the "Authors" table.
SqlDataAdapter myCommand = new SqlDataAdapter("SELECT " +
" * FROM Authors", myConnection);
// Create and fill a DataSet.
DataSet ds = new DataSet();
myCommand.Fill(ds);
// Bind MyDataGrid to the DataSet. MyDataGrid is the ID for the
// DataGrid control in the HTML section.
DataView source = new DataView(ds.Tables[0]);
MyDataGrid.DataSource = source ;
MyDataGrid.DataBind();
}
</script>

<body>
<%-- Display the DataGrid information in the body of the page. --%>
<h3><font face="Verdana">Simple SELECT to a DataGrid Control
</font></h3>
<%-- Display the data in a DataGrid. --%>
<ASP:DataGrid id="MyDataGrid" runat="server"
Width="700"
BackColor="#ccccff"
BorderColor="black"
ShowFooter="false"
CellPadding=3
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
EnableViewState="false"
/>
</body>
</html>

分享到:
评论

相关推荐

    asp.net数据绑定DataBind使用方法

    简单介绍 DataBindDataBind包括三大方法,Repeater,DataList和DataGrid,这些控件都位于 System.Web.UI.WebControls 命名空间中,从 WebControl 基类中直接或间接派生出来的。这些方法都是通过HTML来显示数据的内容...

    Repeater分页显示

    简单的说,先从控件工具箱中拖入两个label(currten page当前页,count page总页),四个linkbuttion(首/下页/上页/尾页)。 无非就是+1 -1 的问题, ------------------------ using System; using System....

    AJAX,初学者最实用

    using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //this....

    C#自定义分页控件

    二、AspNetPager支持各种数据绑定控件GridView、DataGrid、DataList、Repeater以及自定义的数据绑定控件的分页功能十分强大。 三、AspNetPager分页控件本身并不显示任何数据,而只显示分页导航元素,数据在页面上的...

    一个简单的课程投票例子

    using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, ...

    简单新闻管理系统v适合.net初学者

    using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { LoginAcc....

    ASP.NET 分页 二 支持 排序

    private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e) { DataGrid1.CurrentPageIndex = e.NewPageIndex ; BindGrid(e.NewPageIndex ); ...

    学生信息管理系统

    using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class AdminStudent : System.Web.UI.Page { protected static string connStr = ConfigurationManager....

    Asp.Net分页控件【AspNetPager】拖入即用!

    using System.Web.UI.WebControls; //引用命名空间 using System.Data; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { string connstring = "server=baiyi-js\\SQL2005;...

    城市的连动效果

    using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; namespace _0327Province_City_代码实现_ { public partial class _Default : System.Web.UI.Page { ...

    新闻发布系统

    using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.OleDb; public partial class userShow : System.Web.UI.Page { protected void Page_Load(object sender,...

    ASP.NET程序中常用的三十三种代码.txt

    ASP.NET程序中常用的三十三种代码 1. 打开新的窗口并传送参数:  传送参数: response.write("&lt;script&gt;window.open(’*.aspx?id="+this.DropDownList1.SelectIndex+"&id1="+...+"’)&lt;/script&gt;")  接收参数: ...

    jackson-databind-2.9.10.8.jar升级相关jar包

    jackson-databind-2.9.10.8.jar升级相关jar包包含: jackson-module-jaxb-annotations-2.9.10.jar jackson-core-2.9.10.jar jackson-databind-2.9.10.8.jar jackson-annotations-2.9.10.jar jackson-jaxrs-json-...

    jackson-databind-2.12.5-API文档-中文版.zip

    赠送jar包:jackson-databind-2.12.5.jar; 赠送原API文档:jackson-databind-2.12.5-javadoc.jar; 赠送源代码:jackson-databind-...人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。

    .NET中实现客户端联动菜单 (无刷新)

    protected System.Web.UI.WebControls.DropDownList DropDownList3; private void Page_Load(object sender, System.EventArgs e) { Ajax.Utility.RegisterTypeForAjax(typeof(AjaxMethod)); if(!Page....

    repeater实现分页

    repeater实现分页 //对PagedDataSource 对象的相关属性赋值 PagedDataSource objPds = new PagedDataSource(); objPds.DataSource = ds.Tables[0].DefaultView; objPds.AllowPaging = true; objPds.PageSize = 5...

    jackson-databind-2.9.7-API文档-中文版.zip

    赠送jar包:jackson-databind-2.9.7.jar; 赠送原API文档:jackson-databind-2.9.7-javadoc.jar; 赠送源代码:jackson-databind-2.9.7...人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。

    ASP.NET常用代码

    private void grdCustomer_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { //点击表格打开 if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType....

    jackson-databind-2.12.3-API文档-中英对照版.zip

    赠送jar包:jackson-databind-2.12.3.jar; 赠送原API文档:jackson-databind-2.12.3-javadoc.jar;...人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。 双语对照,边学技术、边学英语。

    jackson-databind-2.13.1-API文档-中文版.zip

    赠送jar包:jackson-databind-2.13.1.jar; 赠送原API文档:jackson-databind-2.13.1-javadoc.jar; 赠送源代码:jackson-databind-...人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。

Global site tag (gtag.js) - Google Analytics