RadGrid for ASP.NET

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


Occurs when a item is created in a Telerik RadGrid control.

 

Example

The following code example demonstrates how to use the ItemCreated event to store the index of the item being created in the CommandArgument property of a LinkButton control contained in the item. This allows you to determine the index of the item that contains the LinkButton control when the user clicked the button.  

Event Data

The event handler receives an argument of type GridItemEventArgs containing data related to this event. The following GridItemEventArgs properties provide information specific to this event.

PropertyDescription
Canceled Set to true to cancel the default event execution, if available. The ItemCreated and ItemDataBound events cannot be cancelled.
EventInfo Event info object. Cast to derrived classes to obtain the appropriate instance
Item  
 

 

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

Syntax

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

Event Data

The event handler receives an argument of type GridItemEventArgs containing data related to this event. The following GridItemEventArgs properties provide information specific to this event.

PropertyDescription
Canceled Set to true to cancel the default event execution, if available. The ItemCreated and ItemDataBound events cannot be cancelled.
EventInfo Event info object. Cast to derrived classes to obtain the appropriate instance
Item  

Example

The following code example demonstrates how to use the ItemCreated event to store the index of the item being created in the CommandArgument property of a LinkButton control contained in the item. This allows you to determine the index of the item that contains the LinkButton control when the user clicked the button.
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

Before the Telerik RadGrid control can be rendered, a GridItem object must be created for each row in the control. The ItemCreated event is raised when each row in the Telerik RadGrid control is created. This allows you to provide an event-handling method that performs a custom routine, such as adding custom content to a item, whenever this event occurs.

A GridItemEventArgs object is passed to the event-handling method, which allows you to access the properties of the row being created. You can determine which item type (header item, data pager item, and so on) is being bound by using the Item.ItemType property.

Note that the changes made to the item control and its children at this stage does not persist into the ViewState.

Requirements

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

See Also