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

radgrid ItemDataBound event fire twice is it bug?

1 Answer 253 Views
Grid
This is a migrated thread and some comments may be shown as answers.
zpc
Top achievements
Rank 1
zpc asked on 04 Jun 2010, 09:36 AM
Test code below:

aspx
<telerik:RadGrid runat="server" ID="rgContents" AutoGenerateColumns="false"   
        AllowPaging="true" AllowCustomPaging="true" PageSize="10"   
        OnPageIndexChanged="rgContents_PageIndexChanged"   
        OnItemDataBound="rgContents_ItemDataBound">  
        <MasterTableView> 
            <Columns> 
                <telerik:GridTemplateColumn> 
                    <ItemTemplate> 
                        Times: <asp:Literal ID="ltlTimes" runat="server" /> 
                    </ItemTemplate> 
                </telerik:GridTemplateColumn> 
            </Columns> 
        </MasterTableView> 
        <PagerStyle Mode="NextPrevNumericAndAdvanced" /> 
    </telerik:RadGrid> 

asp.cs
private int Times  
    {  
        get 
        {  
            return ViewState["Times"] != null ? (int)ViewState["Times"] : 0;  
        }  
        set 
        {  
            ViewState["Times"] = value;  
        }  
    }  
 
    protected void Page_Load(object sender, EventArgs e)  
    {  
        if (!IsPostBack)  
        {  
            Bind();  
        }  
    }  
 
    protected void rgContents_PageIndexChanged(object source, GridPageChangedEventArgs e)  
    {  
        rgContents.CurrentPageIndex = e.NewPageIndex;  
        Bind();  
    }  
 
    protected void rgContents_ItemDataBound(object sender, GridItemEventArgs e)  
    {  
        if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)  
        {  
            Times = Times + 1;  
            Literal ltlTimes = e.Item.FindControl("ltlTimes"as Literal;  
            ltlTimes.Text = Times.ToString();  
        }  
    }  
 
    private void Bind()  
    {  
        var biz = new ContentBiz();  
        var contents = biz.GetAll();  
 
        int pageSize = rgContents.PageSize;  
        int start = rgContents.CurrentPageIndex * pageSize;  
        int count = contents.Count();  
 
        var dataSource = contents.Skip(start).Take(pageSize);  
 
        rgContents.VirtualItemCount = count;  
        rgContents.DataSource = dataSource;  
        rgContents.DataBind();  
    } 

when I change page index post back will fire itemdatabound twice.
I am call rgContents.DataBind() in the rgContents_PageIndexChanged, this line code will cause that.
and when use NeedDataSource will correct, but above code why cannot work correct?

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 04 Jun 2010, 11:48 AM
Hello,

 I think the problem is because of calling the DataBind() method after setting the DataSource property. You can refer the Simple data binding demo for more information.

Regards,
Shinu.
Tags
Grid
Asked by
zpc
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or