Compartilhar via


Repeater.OnDataBinding(EventArgs) Método

Definição

Aciona o evento DataBinding.

protected:
 override void OnDataBinding(EventArgs ^ e);
protected override void OnDataBinding (EventArgs e);
override this.OnDataBinding : EventArgs -> unit
Protected Overrides Sub OnDataBinding (e As EventArgs)

Parâmetros

e
EventArgs

Um objeto EventArgs que contém os dados do evento.

Exemplos

Os exemplos de código a seguir demonstram como substituir o OnDataBinding método em um controle de servidor personalizado para que, se o argumento de evento for um tipo de comando, o Repeater controle sempre chamará e OnItemCommand gerará bolhas no evento ou retornará false e cancelará o evento.

<%@ 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 - OnDataBinding - 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 - OnDataBinding - C# Example</h3>
      <aspSample:CustomRepeaterOnDataBinding 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:CustomRepeaterOnDataBinding>
    </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 - OnDataBinding - 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 - OnDataBinding - VB.NET Example</h3>
            
            <aspSample:CustomRepeaterOnDataBinding 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:CustomRepeaterOnDataBinding>
        </form>
    </body>
</html>
using System.Web;
using System.Security.Permissions;

namespace Samples.AspNet.CS.Controls
{
  [AspNetHostingPermission(SecurityAction.Demand, Level=AspNetHostingPermissionLevel.Minimal)]
  public sealed class CustomRepeaterOnDataBinding : System.Web.UI.WebControls.Repeater
  {
    protected override void OnDataBinding(System.EventArgs e)
    {
      // Raise the OnDataBinding event.
      base.OnDataBinding(e);

      // Clear out the controls collection and child viewstate.
      this.Controls.Clear();
      this.ClearChildViewState();

      // Create a new control hierarchy.
      this.CreateControlHierarchy(true);
      this.ChildControlsCreated = true;
    }
  }
}
Imports System.Web
Imports System.Security.Permissions

Namespace Samples.AspNet.VB.Controls
    <AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
    Public NotInheritable Class CustomRepeaterOnDataBinding
        Inherits System.Web.UI.WebControls.Repeater

        Protected Overrides Sub OnDataBinding(ByVal e As System.EventArgs)
            ' Raise the OnDataBinding event.
            MyBase.OnDataBinding(e)

            ' Clear out the controls collection and child viewstate.
            Me.Controls.Clear()
            Me.ClearChildViewState()

            ' Create a new control hierarchy.
            Me.CreateControlHierarchy(True)
            Me.ChildControlsCreated = True
        End Sub
    End Class
End Namespace

Aplica-se a

Confira também