RadGrid for ASP.NET

RadGrid Send comments on this topic.
ItemCommand Event
See Also  Example
Telerik.WebControls Namespace > RadGrid Class : ItemCommand Event


Occurs when a button is clicked in a Telerik RadGrid control.

 

Example

The following code example demonstrates how to use the ItemCommand event to add the name of a customer from a Telerik RadGrid control to a ListBox control when a item's Add button is clicked.  
 

 

Namespace: Telerik.WebControls
Assembly: RadGrid (in RadGrid.dll)

Syntax

Visual Basic (Declaration) 
Public Event ItemCommand() As GridCommandEventHandler
Visual Basic (Usage)Copy Code
Dim instance As RadGrid
Dim handler As GridCommandEventHandler
 
AddHandler instance.ItemCommand, handler
C# 
public event GridCommandEventHandler ItemCommand()
 

Example

The following code example demonstrates how to use the ItemCommand event to add the name of a customer from a Telerik RadGrid control to a ListBox control when a item's Add button is clicked.
Visual BasicCopy Code
<%@ Page Language="VB" <see cref="> <"/>@ Register Assembly="RadGrid.Net2" Namespace="Telerik.WebControls" TagPrefix="rad" %>

<!DOCTYPE html Public "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">


    Protected Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As Telerik.WebControls.GridItemEventArgs)
        If e.Item.ItemType = GridItemType.AlternatingItem Or e.Item.ItemType = GridItemType.Item Then
            Dim item As Telerik.WebControls.GridDataItem
            item = e.Item
            Dim LinkButton1 As LinkButton
            LinkButton1 = item("LinkColumn").FindControl("LinkButton1")
            LinkButton1.CommandArgument = e.Item.ItemIndex.ToString()
        End If
    End Sub

    Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As Telerik.WebControls.GridCommandEventArgs)
        ' If multiple buttons are used in a Telerik RadGrid control, use the
        ' CommandName property to determine which button was clicked.
        If e.CommandName = "Add" Then

            ' Convert the row index stored in the CommandArgument
            ' property to an Integer.
            Dim index As Integer = Convert.ToInt32(e.CommandArgument)

            ' Retrieve the item that contains the button clicked
            ' by the user from the Items collection.
            Dim item As Telerik.WebControls.GridDataItem = RadGrid1.Items(index)

            ' Create a new ListItem object for the customer in the item.
            Dim nitem As New ListItem()
            nitem.Text = Server.HtmlDecode(item("CustomerID").Text)

            ' If the customer is not already in the ListBox, add the ListItem
            ' object to the Items collection of the ListBox control.
            If Not CustomersListBox.Items.Contains(nitem) Then

                CustomersListBox.Items.Add(nitem)

            End If

        End If
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <rad:RadGrid
            DataSourceID="SqlDataSource1"
            ID="RadGrid1"
            runat="server" OnItemCreated="RadGrid1_ItemCreated" OnItemCommand="RadGrid1_ItemCommand">
            <MasterTableView>
                <Columns>
                    <rad:GridTemplateColumn
                        UniqueName="LinkColumn"
                        HeaderText="LinkColumn">
                        <ItemTemplate>
                            <asp:LinkButton CommandName="Add" Text="click" ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>
                        </ItemTemplate>
                    </rad:GridTemplateColumn>
                </Columns>
            </MasterTableView>
        </rad:RadGrid>
        <asp:listbox id="CustomersListBox" runat="server"/>
        <!-- This example uses Microsoft SQL Server And connects -->
        <!-- To the Northwind sample database. Use an ASP.NET -->
        <!-- expression To retrieve the connection String value -->
        <!-- from the Web.config file. -->
        <asp:SqlDataSource
            ID="SqlDataSource1"
            runat="server"
            ConnectionString="<<see cref="NorthwindConnectionString">$ ConnectionStrings</see>>"
            SelectCommand="SELECT TOP 5 [CustomerID], [ContactName], [CompanyName] FROM [Customers]">
        </asp:SqlDataSource>
    </div>
    
</body>
</html>

Remarks

The ItemCommand event is raised when a button is clicked in the Telerik RadGrid control. This allows you to provide an event-handling method that performs a custom routine whenever this event occurs.

Buttons within a Telerik RadGrid control can also invoke some of the built-in functionality of the control. Fires if any control inside Telerik RadGrid rises a bubble event. This can be a command button (like Edit, Update button, Expand/Collapse of an items) The command arguemtn carries a reference to the item which rised the event, the command name and argument object.

A GridCommandEventArgs object is passed to the event-handling method, which allows you to determine the command name and command argument of the button clicked.

Requirements

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

See Also