This is a migrated thread and some comments may be shown as answers.

RadGrid ItemDataBound Event Firing Twice

1 Answer 419 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sam
Top achievements
Rank 2
Sam asked on 09 Feb 2016, 01:35 AM

Hello Support,

I tried searching forums regarding the itemdatabound event being fired twice for a radgrid. But no solutions proved conclusive. So I have created an example project  with a single page.

1 RadScriptManager
1 SQLDataSource
1 RadGrid (Telerik v 2015.2.826.45)

SQLDatasource connecting to a table with 5 rows

id desc percent
1   A   10
2   B   20
3   C   30
4   D   40
5   E   50

ItemDataBound was seen to fire 12 times twice for each row. (+1 for header and +1 for footer)  

Please explain how can I restrict this to fire once for each row.

Code for .vb , .aspx are included. The example project is also attached along with screenshots for runtime and database.

.vb

Imports Telerik.Web.UI
 
Public Class _default
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Session("ctr") = 0
    End Sub
 
    Private Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs) Handles RadGrid1.ItemDataBound
        Session("ctr") += 1
        Debug.WriteLine("ItemDataBound: " & Session("ctr"))
    End Sub
 
End Class

 

 .aspx

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="default.aspx.vb" Inherits="RadGrid_ItemDatabound._default" %>
 
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>
 
<!DOCTYPE html>
 
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     
        <telerik:RadScriptManager ID="RadScriptManager1" Runat="server">
        </telerik:RadScriptManager>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MySQLConnectionString %>" ProviderName="<%$ ConnectionStrings:MySQLConnectionString.ProviderName %>" SelectCommand="Select * from temp"></asp:SqlDataSource>
        <telerik:RadGrid ID="RadGrid1" runat="server" CellSpacing="-1" DataSourceID="SqlDataSource1" GridLines="Both" GroupPanelPosition="Top">
            <MasterTableView AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
                <Columns>
                    <telerik:GridBoundColumn DataField="id" DataType="System.Int32" FilterControlAltText="Filter id column" HeaderText="id" SortExpression="id" UniqueName="id">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="desc" FilterControlAltText="Filter desc column" HeaderText="desc" SortExpression="desc" UniqueName="desc">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="percent" DataType="System.Int32" FilterControlAltText="Filter percent column" HeaderText="percent" SortExpression="percent" UniqueName="percent">
                    </telerik:GridBoundColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
    </div>
    </form>
</body>
</html>

 

Debug Output:

ItemDataBound: 1
ItemDataBound: 2
ItemDataBound: 3
ItemDataBound: 4
ItemDataBound: 5
ItemDataBound: 6
ItemDataBound: 7
ItemDataBound: 8
ItemDataBound: 9
ItemDataBound: 10
ItemDataBound: 11
ItemDataBound: 12

 

 

1 Answer, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 10 Feb 2016, 03:18 PM
Hi Sam,

I have replied to your query in the support ticket you have submitted. I suggest we continue the conversation there.

On a side note, please avoid submitting duplicate thread. This way we can keep better track or your support history and provide answers faster.

I will paste my reply from the ticket in case someone has similar question.



Note that the ItemDataBound event is fired for every item in the grid after it is data bound. Also, there is a GridEditFormItem generated automatically for every GridDataItem.

This is why you are seeing ItemDataBound fire twice for each row. It is triggered for the GridDataItem and then for the GridEditFormItem.

In order to distinguish between the items you can use a condition in the handler. Thus, you can execute custom logic only once depending on the type of the item.

Copy Code
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
     
    if (e.Item is GridDataItem)
    {
        // data item
    }
     
     
    if (e.Item is GridEditFormItem)
    {
        // edit form item
    }
}



Regards,
Viktor Tachev
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
Sam
Top achievements
Rank 2
Answers by
Viktor Tachev
Telerik team
Share this question
or