Repeater.DataBind 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將 Repeater 控制項和它的所有子控制項繫結至指定的資料來源。
public:
override void DataBind();
public override void DataBind ();
override this.DataBind : unit -> unit
Public Overrides Sub DataBind ()
範例
下列程式碼範例示範如何在自訂伺服器控制項中覆寫 DataBind 方法,讓它一律在自訂 Repeater 伺服器控制項中引發資料系結事件。
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS.Controls" Assembly="Samples.AspNet.CS" %>
<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Custom Repeater - DataBind - C# Example</title>
<script language="C#" runat="server">
void Page_Load(Object Sender, EventArgs e)
{
if (!IsPostBack)
{
ArrayList values = new ArrayList();
values.Add(new PositionData("Microsoft", "Msft"));
values.Add(new PositionData("Intel", "Intc"));
values.Add(new PositionData("Dell", "Dell"));
Repeater1.DataSource = values;
Repeater1.DataBind();
}
}
public class PositionData
{
private string name;
private string ticker;
public PositionData(string name, string ticker)
{
this.name = name;
this.ticker = ticker;
}
public string Name
{
get
{
return name;
}
}
public string Ticker
{
get
{
return ticker;
}
}
}
</script>
</head>
<body>
<form id="Form1" method="post" runat="server">
<h3>Custom Repeater - DataBind - C# Example</h3>
<aspSample:CustomRepeaterDataBind id="Repeater1" runat="server">
<HeaderTemplate>
<table border="1" cellpadding="4" cellspacing="0">
<tr>
<th>Company</th>
<th>Symbol</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td> <%# DataBinder.Eval(Container.DataItem, "Name") %></td>
<td> <%# DataBinder.Eval(Container.DataItem, "Ticker") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</aspSample:CustomRepeaterDataBind>
</form>
</body>
</html>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB.Controls" Assembly="Samples.AspNet.VB" %>
<%@ Page Language="VB" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Custom Repeater - DataBind - VB.NET Example</title>
<script language="VB" runat="server">
Sub Page_Load(Sender As Object, e As EventArgs)
If Not IsPostBack Then
Dim values As New ArrayList()
values.Add(New PositionData("Microsoft", "Msft"))
values.Add(New PositionData("Intel", "Intc"))
values.Add(New PositionData("Dell", "Dell"))
Repeater1.DataSource = values
Repeater1.DataBind()
End If
End Sub
Public Class PositionData
Private myName As String
Private myTicker As String
Public Sub New(newName As String, newTicker As String)
Me.myName = newName
Me.myTicker = newTicker
End Sub
Public ReadOnly Property Name() As String
Get
Return myName
End Get
End Property
Public ReadOnly Property Ticker() As String
Get
Return myTicker
End Get
End Property
End Class
</script>
</head>
<body>
<form id="Form1" method="post" runat="server">
<h3>Custom Repeater - DataBind - VB.NET Example</h3>
<aspSample:CustomRepeaterDataBind id="Repeater1" runat="server" >
<HeaderTemplate>
<table border="1" cellpadding="4" cellspacing="0">
<tr>
<th>Company</th>
<th>Symbol</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td> <%# DataBinder.Eval(Container.DataItem, "Name") %></td>
<td> <%# DataBinder.Eval(Container.DataItem, "Ticker") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</aspSample:CustomRepeaterDataBind>
</form>
</body>
</html>
using System.Web;
using System.Security.Permissions;
namespace Samples.AspNet.CS.Controls
{
[AspNetHostingPermission(SecurityAction.Demand, Level=AspNetHostingPermissionLevel.Minimal)]
public sealed class CustomRepeaterDataBind : System.Web.UI.WebControls.Repeater
{
public override void DataBind()
{
// Raise the DataBinding event.
this.OnDataBinding(System.EventArgs.Empty);
}
}
}
Imports System.Web
Imports System.Security.Permissions
Namespace Samples.AspNet.VB.Controls
<AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
Public NotInheritable Class CustomRepeaterDataBind
Inherits System.Web.UI.WebControls.Repeater
Public Overrides Sub DataBind()
' Raise the DataBinding event.
Me.OnDataBinding(System.EventArgs.Empty)
End Sub
End Class
End Namespace
備註
使用 方法, DataBind 將 屬性所 DataSource 指定的資料來源系結至 Repeater 控制項。 當您將資料來源系結至 Repeater 控制項時,資料來源中的資訊會顯示在 控制項中。
方法 DataBind 通常也會用來在更新資料來源中的資訊之後,同步處理資料來源和資料清單控制項。 這可讓資料來源中的任何變更也會在資料清單控制項中更新。
如果 控制項的 Repeater 資料來源是由 DataSourceID 屬性指定,則不需要明確呼叫 DataBind 方法。 ASP.NET 會自動呼叫這個方法,以將指定的資料來源控制項系結至 Repeater 控制項。