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

Why does ItemDataBound() run twice for each row in the grid?

12 Answers 1197 Views
Grid
This is a migrated thread and some comments may be shown as answers.
DeadlyAccurate
Top achievements
Rank 1
DeadlyAccurate asked on 05 Jun 2008, 06:10 PM
I was getting performance issues, and I finally managed to trace it back to the code in ItemDataBound(). For some reason, every record was being databound four times. So I grew curious.

I created a new project, threw a radgrid on it along with a SQLDataSource that ran a simple 53-record SELECT stored proc. In ItemDataBound() I set:

Debug.WriteLine("ItemDataBound");

Then I did the same with a basic GridView. In RowDataBound(), I set:

Debug.WriteLine("RowDataBound");


When ItemDataBound ran, it came up with 108 lines (106 for the records 1 for the header, and one for the footer). When I ran RowDataBound(), it came up with 55 lines (not sure where the extra one is coming from. Possibly a footer that's not visible?)

So why is it running twice? (Or in my case, four times?)

ETA: Results from those debug lines with e.Item.ItemIndex/e.Row.RowIndex added:

-1 ItemDataBound
0 ItemDataBound
0 ItemDataBound
1 ItemDataBound
1 ItemDataBound
2 ItemDataBound
2 ItemDataBound
3 ItemDataBound
3 ItemDataBound
4 ItemDataBound
4 ItemDataBound
5 ItemDataBound
5 ItemDataBound
6 ItemDataBound
6 ItemDataBound
7 ItemDataBound
7 ItemDataBound
8 ItemDataBound
8 ItemDataBound
9 ItemDataBound
9 ItemDataBound
10 ItemDataBound
10 ItemDataBound
11 ItemDataBound
11 ItemDataBound
12 ItemDataBound
12 ItemDataBound
13 ItemDataBound
13 ItemDataBound
14 ItemDataBound
14 ItemDataBound
15 ItemDataBound
15 ItemDataBound
16 ItemDataBound
16 ItemDataBound
17 ItemDataBound
17 ItemDataBound
18 ItemDataBound
18 ItemDataBound
19 ItemDataBound
19 ItemDataBound
20 ItemDataBound
20 ItemDataBound
21 ItemDataBound
21 ItemDataBound
22 ItemDataBound
22 ItemDataBound
23 ItemDataBound
23 ItemDataBound
24 ItemDataBound
24 ItemDataBound
25 ItemDataBound
25 ItemDataBound
26 ItemDataBound
26 ItemDataBound
27 ItemDataBound
27 ItemDataBound
28 ItemDataBound
28 ItemDataBound
29 ItemDataBound
29 ItemDataBound
30 ItemDataBound
30 ItemDataBound
31 ItemDataBound
31 ItemDataBound
32 ItemDataBound
32 ItemDataBound
33 ItemDataBound
33 ItemDataBound
34 ItemDataBound
34 ItemDataBound
35 ItemDataBound
35 ItemDataBound
36 ItemDataBound
36 ItemDataBound
37 ItemDataBound
37 ItemDataBound
38 ItemDataBound
38 ItemDataBound
39 ItemDataBound
39 ItemDataBound
40 ItemDataBound
40 ItemDataBound
41 ItemDataBound
41 ItemDataBound
42 ItemDataBound
42 ItemDataBound
43 ItemDataBound
43 ItemDataBound
44 ItemDataBound
44 ItemDataBound
45 ItemDataBound
45 ItemDataBound
46 ItemDataBound
46 ItemDataBound
47 ItemDataBound
47 ItemDataBound
48 ItemDataBound
48 ItemDataBound
49 ItemDataBound
49 ItemDataBound
50 ItemDataBound
50 ItemDataBound
51 ItemDataBound
51 ItemDataBound
52 ItemDataBound
52 ItemDataBound
-1 ItemDataBound



-1 RowDataBound
0 RowDataBound
1 RowDataBound
2 RowDataBound
3 RowDataBound
4 RowDataBound
5 RowDataBound
6 RowDataBound
7 RowDataBound
8 RowDataBound
9 RowDataBound
10 RowDataBound
11 RowDataBound
12 RowDataBound
13 RowDataBound
14 RowDataBound
15 RowDataBound
16 RowDataBound
17 RowDataBound
18 RowDataBound
19 RowDataBound
20 RowDataBound
21 RowDataBound
22 RowDataBound
23 RowDataBound
24 RowDataBound
25 RowDataBound
26 RowDataBound
27 RowDataBound
28 RowDataBound
29 RowDataBound
30 RowDataBound
31 RowDataBound
32 RowDataBound
33 RowDataBound
34 RowDataBound
35 RowDataBound
36 RowDataBound
37 RowDataBound
38 RowDataBound
39 RowDataBound
40 RowDataBound
41 RowDataBound
42 RowDataBound
43 RowDataBound
44 RowDataBound
45 RowDataBound
46 RowDataBound
47 RowDataBound
48 RowDataBound
49 RowDataBound
50 RowDataBound
51 RowDataBound
52 RowDataBound
-1 RowDataBound

12 Answers, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 09 Jun 2008, 07:47 AM
Hi,

In order to further troubleshoot the sample you may also want to check the itemtype - i suspect that one response is for the dataItem, and one for the editformitem associated with the corresponding item.

Regards,
Yavor
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
newbie
Top achievements
Rank 1
answered on 25 Jul 2008, 11:22 PM
I always render my grid in edit mode.
I am facing a similar problem.

The ItemDataBound event gets called twice.

I am calculating the total to be displayed in the footer in the ItemdataBound event.
and the total always gets doubled.

My code looks like this:

protected

void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)

{

if
(e.Item is GridEditableItem && (e.Item as GridEditableItem).IsInEditMode)

{

GridEditableItem editItem = e.Item as GridEditableItem;

RadNumericTextBox

txt01 = (editItem.FindControl("txt01") as RadNumericTextBox);

sum += (

string.IsNullOrEmpty(txt01.Text) ? 0 : double.Parse(txt01.Text));

}

}

What am I doing wrong?

0
Sebastian
Telerik team
answered on 28 Jul 2008, 07:06 AM
Hi newbie,

Can you please check whether the code from the conditional block (inside the ItemDataBound handler) gets executed only once for each edited row in RadGrid? The doubled event firing might happen due to explicit Rebind() call somewhere in your code (which will force rebind action for the grid) or if you attached the ItemDataBound event twice.

Best regards,
Stephen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
newbie
Top achievements
Rank 1
answered on 28 Jul 2008, 04:08 PM
I do a rebind in my ItemCommand event after an insert or an update.
Hence my thought is that the ItemDataBound is called twice.

I need to rebind because my dataset changes and I need to display the new data.
I calculate footer total in the ItemdataBound event.
Since the ItemdataBound event is being called twice my total gets doubled.

Can you suggest something.
0
newbie
Top achievements
Rank 1
answered on 28 Jul 2008, 04:42 PM
I guess I don't need a Rebind() with the inserts/updates at all.
0
Princy
Top achievements
Rank 2
answered on 29 Jul 2008, 06:25 AM
Hi Newbie,

Have a look at the following help article.
Commands that invoke Rebind() implicitly

Princy.
0
Lucania
Top achievements
Rank 1
answered on 21 Jul 2011, 07:19 AM
The above link seems to be stale, this looks like the article: Commands that invoke Rebind() implicitly

ROSCO
0
Plaban
Top achievements
Rank 1
answered on 12 Mar 2013, 10:59 AM
Hi Newbi,

I am facing the same issue. What have u done to solve this?
0
Princy
Top achievements
Rank 2
answered on 22 Apr 2013, 11:26 AM
Hi,

I guess you are calling Rebind() method after insert and update. RadGrid  will automatically refresh after Update/Insert/Delete operation if you binding the Grid in the NeedDataSource event. Try removing the Rebind() if it is the case. Please elaborate the senario if it doesn't help.

Thanks,
Princy.
0
iTegra
Top achievements
Rank 2
answered on 22 May 2013, 05:16 PM
My code binds datasource just once, but ItemDataBound event is fired twice.
There is no 
explicit call Rebind() is made. 


lv.DataSource = documents;
lv.DataBind();

I commented out the second line. Surprisingly it worked fine, ItemDataBound 
event fired just once. It can happen if DataSource property has triggered Rebind() call internally.
In such case this behavior is unexpected and undocumented. I call it software bug.

Kind regards,
Vlad
0
Princy
Top achievements
Rank 2
answered on 23 May 2013, 05:23 AM
Hi,

I guess you are binding the values to radgrid in the Page_Load event,please use the NeedDataSource event to bind the values
to radgrid,it is the best way and it avoids calling the ItemDataBound event twice.

Please have a look at the following documentation for more help.
Advanced Data-binding (using NeedDataSource event)

Thanks
Princy
0
Loki
Top achievements
Rank 1
answered on 19 Mar 2016, 07:19 AM

Hi Newbie,

after many efforts found a soluation,

int total;
protected void grdAddList_ItemDataBound(object sender, GridItemEventArgs e)
{
try {
if (e.Item is GridDataItem) {
GridDataItem dataItem = e.Item as GridDataItem;
Label lblgrdAmount = (Label)dataItem.FindControl("lbl_grd_Amount");
if (!string.IsNullOrEmpty(lblgrdAmount.Text)) {
int fieldValue = int.Parse(lblgrdAmount.Text);
total = (total + fieldValue);
}
//dataItem("Amount").Text = total.ToString()
}

if (e.Item is GridFooterItem) {
Label lblTotalamount = (Label)e.Item.FindControl("lblTotal");
lblTotalamount.Text = "Total :" + total.ToString();
}

} catch (Exception ex) {
string strExpMsg = null;
strExpMsg = className + "." + MethodBase.GetCurrentMethod().Name + "()" + "->" + ex.Message.ToString();
ExceptionHandler(strExpMsg, ex, this.strUserName);
}
}

 

Don't use:(object sender, Telerik.Web.UI.GridItemEventArgs e)

Use : (object sender, GridItemEventArgs e)

 

protected void grdAddList_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{

try {
if (e.Item is GridDataItem) {
GridDataItem dataItem = e.Item as GridDataItem;
Label lblgrdAmount = (Label)dataItem.FindControl("lbl_grd_Amount");
if (!string.IsNullOrEmpty(lblgrdAmount.Text)) {
int fieldValue = int.Parse(lblgrdAmount.Text);
total = (total + fieldValue);
}
}

if (e.Item is GridFooterItem) {
Label lblTotalamount = (Label)e.Item.FindControl("lblTotal");
lblTotalamount.Text = total.ToString();
}

} catch (Exception ex) {
string strExpMsg = null;
strExpMsg = className + "." + MethodBase.GetCurrentMethod().Name + "()" + "->" + ex.Message.ToString();
ExceptionHandler(strExpMsg, ex, this.strUserName);
}
}

protected void grdAddList_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{

try {
if (e.Item is GridDataItem) {
GridDataItem dataItem = e.Item as GridDataItem;
Label lblgrdAmount = (Label)dataItem.FindControl("lbl_grd_Amount");
if (!string.IsNullOrEmpty(lblgrdAmount.Text)) {
int fieldValue = int.Parse(lblgrdAmount.Text);
total = (total + fieldValue);
}
}

if (e.Item is GridFooterItem) {
Label lblTotalamount = (Label)e.Item.FindControl("lblTotal");
lblTotalamount.Text = total.ToString();
}

} catch (Exception ex) {
string strExpMsg = null;
strExpMsg = className + "." + MethodBase.GetCurrentMethod().Name + "()" + "->" + ex.Message.ToString();
ExceptionHandler(strExpMsg, ex, this.strUserName);
}
}

 

let me know if any concern

thanks,

Lokesh

Tags
Grid
Asked by
DeadlyAccurate
Top achievements
Rank 1
Answers by
Yavor
Telerik team
newbie
Top achievements
Rank 1
Sebastian
Telerik team
Princy
Top achievements
Rank 2
Lucania
Top achievements
Rank 1
Plaban
Top achievements
Rank 1
iTegra
Top achievements
Rank 2
Loki
Top achievements
Rank 1
Share this question
or