Repeater.DataBind Método
Definição
public:
override void DataBind();
public override void DataBind ();
override this.DataBind : unit -> unit
Public Overrides Sub DataBind ()
Exemplos
O exemplo de código a seguir demonstra como substituir o DataBind método em um controle de servidor personalizado para que ele sempre gere um evento de vinculação de dados em um Repeater controle de servidor personalizado.The following code example demonstrates how to override the DataBind method in a custom server control so that it always raises a data binding event in a custom Repeater server control.
<%@ 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
Comentários
Use o DataBind método para associar a fonte de dados especificada pela DataSource propriedade ao Repeater controle.Use the DataBind method to bind the data source specified by the DataSource property to the Repeater control. Quando você associa uma fonte de dados ao Repeater controle, as informações na fonte de dados são exibidas no controle.When you bind a data source to the Repeater control, the information in the data source is displayed in the control.
O DataBind método também é usado normalmente para sincronizar a fonte de dados e um controle de listagem de dados depois que as informações na fonte de dados são atualizadas.The DataBind method is also commonly used to synchronize the data source and a data listing control after information in the data source is updated. Isso permite que todas as alterações na fonte de dados também sejam atualizadas em um controle de listagem de dados.This allows any changes in the data source to also be updated in a data listing control.
Se a fonte de dados do Repeater controle for especificada pela DataSourceID propriedade, você não precisará chamar o DataBind método explicitamente.If the data source for the Repeater control is specified by the DataSourceID property, you do not need to explicitly call the DataBind method. ASP.NET chama esse método automaticamente para associar o controle de fonte de dados especificado ao Repeater controle.ASP.NET calls this method automatically to bind the specified data source control to the Repeater control.