Export (0) Print
Expand All

Repeater.DataBind Method ()

This method supports the .NET Framework infrastructure and is not intended to be used directly from your code.

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

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

public override void DataBind ()
public void DataBind ()
public override function DataBind ()

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.

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" %>
<HTML>
	<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.JSL.Controls" Assembly="Samples.AspNet.JSL" %>
<%@ Page Language="VJ#" AutoEventWireup="True" %>
<HTML>
	<HEAD>
		<title>Custom Repeater - DataBind - VJ# Example</title>
    <script language="VJ#" runat="server">
        void Page_Load(Object Sender, EventArgs e) 
        {
            if (!get_IsPostBack()) {
                ArrayList values = new ArrayList();
                values.Add(new PositionData("Microsoft", "Msft"));
                values.Add(new PositionData("Intel", "Intc"));
                values.Add(new PositionData("Dell", "Dell"));
                Repeater1.set_DataSource(values);
                Repeater1.DataBind();
            }
        } //Page_Load

        public class PositionData 
        {
            private String name;
            private String ticker;

            public PositionData(String name, String ticker) 
            {
                this.name = name;
                this.ticker = ticker;
            } //PositionData

            /** @property 
             */
            public String get_Name()
            {
                return name;
            } //get_Name

            /** @property 
             */
            public String get_Ticker()
            {
                return ticker;
            } //get_Ticker
        } //PositionData 
    </script>
	</HEAD>
	<body>
		<form id="Form1" method="post" runat="server">
			<h3>Custom Repeater - DataBind - VJ# 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.get_DataItem(), "Name") %></td>
            <td> <%# DataBinder.Eval(Container.get_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);
    }
  }
}

package Samples.AspNet.JSL.Controls; 

public class CustomRepeaterDataBind
    extends System.Web.UI.WebControls.Repeater
{
    public void DataBind()
    {
        // Raise the DataBinding event.
        this.OnDataBinding(System.EventArgs.Empty);
    } //DataBind
} //CustomRepeaterDataBind

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft