经典的数据绑定使用大全
(一) Repeater控件的使用(扬鹏)
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="t2.WebForm1" %> //头标记
<!DOCTYPE HTML PUBLIC "-//W
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body topmargin="0" leftmargin="0">
<ASP:Repeater id="MyRepeater" runat="server">
<HeaderTemplate>//页眉
<table width="100%" style="font:
<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}") %>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>//页脚
</table>
</FooterTemplate>
</ASP:Repeater>
</body>
</HTML>
Repeater的代码文件
//命名空间(扬鹏)
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data .SqlClient; //数据库命名空间
namespace t2
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Repeater MyRepeater;
private void Page_Load(object sender, System.EventArgs e)
{
SqlConnection myConnection = new SqlConnection("server=.;database=pubs;uid=sa;pwd=5201314" );
//SqlConnection建立数据连接
SqlDataAdapter myCommand = new SqlDataAdapter("select * from Titles", myConnection);
//SqlDataAdapter作为连接DataSet对象和数据源的桥梁,用于从数据源中获取数据,填充DataSet并负责将DataSet中数据的更改写回数据源.(提供SelectCommand InsertCommand DeleteCommand和UpCommand四个对象)
DataSet ds = new DataSet(); //建立DataSet对象
myCommand.Fill(ds, "Titles"); //填充DataSet
this.MyRepeater.DataSource = ds.Tables["Titles"]; //获取DataTable 并把获取的数据赋值给Repeater
this.MyRepeater.DataBind(); //绑定数据库
// 绿色字相当于 DataTable bs=ds.Tables["Titles"];
MyRepeater.DataSource = bs;
MyRepeater.DataBind();
}
}
}
(二)DatalList 控件使用
<%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="t2.WebForm2" %>
<!DOCTYPE HTML PUBLIC "-//W
<HTML>
<HEAD>
<title>WebForm2</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body leftMargin="0" topMargin="0" marginheight="0" marginwidth="0">
<ASP:DataList id="MyDataList" RepeatColumns="2" RepeatDirection="Horizontal" runat="server">
<ItemTemplate>
<div style="padding:15,15,15,15;font-size:
<div style="font:
<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}") %>
<p>
</div>
</ItemTemplate>
</ASP:DataList>
</body>
</HTML>
DataList代码文件和Repeatter一样
浙公网安备 33010602011771号