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

13 posts, 0 answers
  1. DeadlyAccurate
    DeadlyAccurate avatar
    5 posts
    Member since:
    Jan 2008

    Posted 05 Jun 2008 Link to this post

    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

  2. Yavor
    Admin
    Yavor avatar
    11 posts

    Posted 09 Jun 2008 Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. newbie
    newbie avatar
    355 posts
    Member since:
    Feb 2008

    Posted 25 Jul 2008 Link to this post

    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?

  5. Sebastian
    Admin
    Sebastian avatar
    9934 posts

    Posted 28 Jul 2008 Link to this post

    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.
  6. newbie
    newbie avatar
    355 posts
    Member since:
    Feb 2008

    Posted 28 Jul 2008 Link to this post

    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.
  7. newbie
    newbie avatar
    355 posts
    Member since:
    Feb 2008

    Posted 28 Jul 2008 Link to this post

    I guess I don't need a Rebind() with the inserts/updates at all.
  8. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 29 Jul 2008 Link to this post

    Hi Newbie,

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

    Princy.
  9. Lucania
    Lucania  avatar
    65 posts
    Member since:
    May 2011

    Posted 21 Jul 2011 Link to this post

    The above link seems to be stale, this looks like the article: Commands that invoke Rebind() implicitly

    ROSCO
  10. Plaban
    Plaban avatar
    1 posts
    Member since:
    Mar 2013

    Posted 12 Mar 2013 Link to this post

    Hi Newbi,

    I am facing the same issue. What have u done to solve this?
  11. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 22 Apr 2013 Link to this post

    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.
  12. iTegra
    iTegra avatar
    1 posts
    Member since:
    Mar 2013

    Posted 22 May 2013 Link to this post

    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
  13. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 23 May 2013 Link to this post

    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
  14. Loki
    Loki avatar
    4 posts
    Member since:
    May 2015

    Posted 19 Mar Link to this post

    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

Back to Top
UI for ASP.NET Ajax is Ready for VS 2017