Export (0) Print
Expand All
1 out of 2 rated this helpful - Rate this topic

Repeater.OnItemCommand Method

Raises the ItemCommand event.

[Visual Basic]
Protected Overridable Sub OnItemCommand( _
   ByVal e As RepeaterCommandEventArgs _
)
[C#]
protected virtual void OnItemCommand(
 RepeaterCommandEventArgs e
);
[C++]
protected: virtual void OnItemCommand(
 RepeaterCommandEventArgs* e
);
[JScript]
protected function OnItemCommand(
   e : RepeaterCommandEventArgs
);

Parameters

e
The RepeaterCommandEventArgs that contains the event data.

Remarks

Raising an event invokes the event handler through a delegate. For more information, see Raising an Event.

Notes to Inheritors:  When overriding OnItemCommand in a derived class, be sure to call the base class's OnItemCommand method.

Example

[Visual Basic, C#] The following example illustrates a way to handle an ItemCommand event of the Repeater. Information about the button is displayed whenever a Button control within the Repeater is clicked.

[Visual Basic] 
<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
 <head>
    <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

    Sub R1_ItemCommand(Sender As Object, e As RepeaterCommandEventArgs)
        Label2.Text = "The " & CType(e.CommandSource, Button).Text & _
            " button has just been clicked; <br>"
    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>
 
    <h3>Repeater Example</h3>
 
    <form runat=server>
 
       <b>Repeater1:</b>
         
       <p>
         
       <asp:Repeater id=Repeater1 OnItemCommand="R1_ItemCommand" runat="server">
          <HeaderTemplate>
             <table border=1>
                <tr>
                   <td><b>Company</b></td>
                   <td><b>Symbol</b></td>
                </tr>
          </HeaderTemplate>
             
          <ItemTemplate>
             <tr>
                <td> <%# DataBinder.Eval(Container.DataItem, "Name") %> </td>
                <td> <ASP:Button Text=<%# DataBinder.Eval(Container.DataItem, "Ticker") %> runat="server" /></td>
             </tr>
          </ItemTemplate>
             
          <FooterTemplate>
             </table>
          </FooterTemplate>
             
       </asp:Repeater>
       <p>
         
       <asp:Label id=Label2 font-name="Verdana" ForeColor="Green" font-size="10pt" runat="server"/>
    </form>
 </body>
 </html>
 

[C#] 
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
 <head>
    <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();
          }
       }
 
       void R1_ItemCommand(Object Sender, RepeaterCommandEventArgs e) {        
          Label2.Text = "The " + ((Button)e.CommandSource).Text + " button has just been clicked; <br>";
       }    
 
       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>
 
    <h3>Repeater Example</h3>
 
    <form runat=server>
 
       <b>Repeater1:</b>
         
       <p>
         
       <asp:Repeater id=Repeater1 OnItemCommand="R1_ItemCommand" runat="server">
          <HeaderTemplate>
             <table border=1>
                <tr>
                   <td><b>Company</b></td>
                   <td><b>Symbol</b></td>
                </tr>
          </HeaderTemplate>
             
          <ItemTemplate>
             <tr>
                <td> <%# DataBinder.Eval(Container.DataItem, "Name") %> </td>
                <td> <ASP:Button Text=<%# DataBinder.Eval(Container.DataItem, "Ticker") %> runat="server" /></td>
             </tr>
          </ItemTemplate>
             
          <FooterTemplate>
             </table>
          </FooterTemplate>
             
       </asp:Repeater>
       <p>
         
       <asp:Label id=Label2 font-name="Verdana" ForeColor="Green" font-size="10pt" runat="server"/>
    </form>
 </body>
 </html>
 

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

Repeater Class | Repeater Members | System.Web.UI.WebControls Namespace | ItemCommand | RepeaterCommandEventArgs | RepeaterCommandEventHandler

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.