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

HeaderItem vs. DataItem during ItemDataBound event

2 Answers 235 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Daniel
Top achievements
Rank 1
Daniel asked on 16 Apr 2012, 09:21 PM
I've whittled down my project to the following code. If you change the pagesize of the radgrid in this example, the ItemDataBound event will be called each time an item is databound. In my example, I change the HeaderText to the count field, and then I change the first data row's cell to a different color

But only the data item shows the color change. The header item doesn't show any change until the next time I change the page size from the browser, and then it's back at zero where it should have been the first time I changed the page size. In fact, it's always one behind. Why would there be a difference in behavior like this? Is this how it's supposed to work and if so what is the benefit here?

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<head runat="server">
    <title></title>
    <telerik:RadStyleSheetManager id="RadStyleSheetManager1" runat="server" />
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
        <Scripts>
            <%--Needed for JavaScript IntelliSense in VS2010--%>
            <%--For VS2008 replace RadScriptManager with ScriptManager--%>
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" />
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" />
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js" />
        </Scripts>
    </asp:ScriptManager>
    <script type="text/javascript">
        //Put your JavaScript code here.
    </script>
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadGrid1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
                    <telerik:AjaxUpdatedControl ControlID="RadButton1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
            <telerik:AjaxSetting AjaxControlID="RadButton1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
                    <telerik:AjaxUpdatedControl ControlID="RadButton1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    <div>
            <telerik:RadGrid ID="RadGrid1" runat="server" CellSpacing="0" GridLines="None" GroupingEnabled="False"
                ShowStatusBar="True" Skin="Windows7" OnNeedDataSource="RadGrid1_NeedDataSource" AllowSorting="True" AllowCustomPaging="True"
                AllowPaging="True" OnItemDataBound="RadGrid1ControlItemDataBound" PageSize="20" >
                <HeaderContextMenu />
                <PagerStyle Mode="NextPrevAndNumeric" AlwaysVisible="True" PageButtonCount="6" ShowPagerText="False" />
                <ExportSettings HideStructureColumns="true" ExportOnlyData="true" IgnorePaging="true"
                    FileName="FlashSalesExport" OpenInNewWindow="True" />
                <MasterTableView AllowCustomSorting="True" CommandItemDisplay="Top">
                    <CommandItemSettings ShowExportToCsvButton="true" ShowRefreshButton="False" ShowAddNewRecordButton="False" />
                    <PagerStyle PageButtonCount="6" Position="Bottom" Wrap="True" AlwaysVisible="True"
                        ShowPagerText="False" />
                    <HeaderStyle HorizontalAlign="Center" />
                </MasterTableView>
                <FilterMenu EnableImageSprites="False" />
                <EditItemStyle Font-Bold="True" />
            </telerik:RadGrid>
    </div>
    <telerik:RadButton ID="RadButton1" runat="server"
        Text="RadButton">
    </telerik:RadButton>
    </form>
</body>
</html>

using System;
 
using Telerik.Web.UI;
 
public partial class Default : System.Web.UI.Page
{
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        RadGrid1.DataSource = new string[]
        {
            Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
        };
    }
 
    private int Count
    {
        get
        {
            if (Session["Count"] == null)
                Session["Count"] = 0;
            else
                Session["Count"] = (int)Session["Count"] + 1;
 
            return (int)Session["Count"];
        }
    }
 
    protected void RadGrid1ControlItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridHeaderItem)
        {
            GridHeaderItem item = e.Item as GridHeaderItem;
 
            if (item.OwnerTableView.AutoGeneratedColumns.Length > 0)
            {
                item.OwnerTableView.AutoGeneratedColumns[0].HeaderText = Count.ToString();
            }
        }
        else if (e.Item is GridDataItem)
        {
            if (rowcount++ == 0)
                e.Item.Cells[2].ForeColor = System.Drawing.Color.Red;
        }
    }
 
    int rowcount = 0;
}

A second question is how would I get the RadButton to induce a rebind of the RadGrid? I could put code to do that in the Page_Load event but only on a postback, but then I get multiple postbacks and a lot of extra work that I don't need. The button is set to update the grid (and itself) but unless I implement the page_load, I'm not going to get anything.

Any help would be appreciated.


2 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 17 Apr 2012, 09:54 AM
Hello Daniel,


protected void RadGrid1_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
    {
        if (e.Column.UniqueName == "Item")
        {
 
            e.Column.HeaderText = Count.ToString();
        }
    }



Thanks,
Jayesh Goyani
0
Daniel
Top achievements
Rank 1
answered on 17 Apr 2012, 04:16 PM
Jayesh, but if I add this code, then the ColumnCreated fires the first time, but then both the itemdatabound and the columncreated fire on later updates causing the count to index twice.

This seems like more of a workaround to an answer to the actual question of why I'm getting the delayed update.
Tags
Grid
Asked by
Daniel
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Daniel
Top achievements
Rank 1
Share this question
or