Repeater.DataBind Method

Definition

Binds the Repeater control and all its child controls to the specified data source.

C#
public override void DataBind();

Examples

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.

ASP.NET (C#)
<%@ 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>
C#
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);
    }
  }
}

Remarks

Use the DataBind method to bind the data source specified by the DataSource property to the Repeater control. When you bind a data source to the Repeater control, the information in the data source is displayed in the control.

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. This allows any changes in the data source to also be updated in a data listing control.

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 calls this method automatically to bind the specified data source control to the Repeater control.

Applies to

Produkt Verzie
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

See also