Test code below:
aspx
asp.cs
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?
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?