Bug reports Found a bug? Post it here please.
Version 5.x > Bug reports > asp.repeater does not retain data after postback View modes: 
User avatar
Member
Member
vlado - 4/2/2011 9:00:53 AM
   
asp.repeater does not retain data after postback
Steps to reproduce:

1. Create a new Test web part with this simple repeater
Test.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Test.ascx.cs" Inherits="CMSWebParts_Test" %>
<asp:Panel ID="PanelMain" runat="server">
<table cellpadding="0" cellspacing="0" width="40%" align="center" border="2">
<tr>
<td>
<asp:Repeater ID="rptEmployee" runat="server">
<HeaderTemplate>
<table>
<tr>
<th width="100px" align="center">
EmployeeID
</th>
<th width="100px" align="center">
DepartmentID
</th>
<th width="100px" align="center">
Name
</th>
<th width="100px" align="center">
Country
</th>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td width="100px" align="center">
<%# ((Employee)Container.DataItem).EmpID %>
</td>
<td width="100px" align="center">
<%# ((Employee)Container.DataItem).DeptID %>
</td>
<td width="100px" align="center">
<%# ((Employee)Container.DataItem).EmpName %>
</td>
<td width="100px" align="center">
<%# ((Employee)Container.DataItem).Country %>
</td>
</tr>
</table>
</ItemTemplate>
<FooterTemplate>
</table><br />
</FooterTemplate>
</asp:Repeater>
</td>
</tr>
</table>
<asp:Button ID="Button1" runat="server" Text="Button" />
</asp:Panel>


Test.ascx.cs:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CMS.PortalControls;
using CMS.CMSHelper;
using System.Data;

public partial class CMSWebParts_Test : CMSAbstractWebPart
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
rptEmployee.DataSource = EmployeeList.emplst;
rptEmployee.DataBind();
}
}

public class EmployeeList
{
static EmployeeList()
{
emplst = new List<Employee>();
emplst.Add(new Employee() { EmpID = 1, DeptID = 1, EmpName = "Smith", Country = "USA" });
emplst.Add(new Employee() { EmpID = 2, DeptID = 4, EmpName = "Adam", Country = "Japan" });
emplst.Add(new Employee() { EmpID = 3, DeptID = 3, EmpName = "Sara", Country = "China" });
emplst.Add(new Employee() { EmpID = 4, DeptID = 4, EmpName = "Ram", Country = "India" });
emplst.Add(new Employee() { EmpID = 5, DeptID = 3, EmpName = "Kalvin", Country = "UK" });
}
public static List<Employee> emplst { get; set; }
}

public class Employee
{
public int EmpID { get; set; }
public int DeptID { get; set; }
public string EmpName { get; set; }
public string Country { get; set; }
}
}


2. Create a Blank Web Site (no content in it)
3. Register Test web part with the site
4. Place the part on a single page of the blank site
5. Run site. Web part displays a table of Employees
6. Click Button
7. Data disappears after postback
8. Create a new web project in VS2010
9. Create a new page
10. Create a new custom control with exact the same code as above
11. Place the control on the empty page
12. Run site and click the button
13. The repeater retains the data


Vlado

User avatar
Member
Member
kentico_michal - 4/7/2011 3:17:42 AM
   
RE:asp.repeater does not retain data after postback
Hi Vlado,

Could you please try to use the User Control web part to insert your own control on the page instead of creating a new web part.

It seems to be some kind of life cycle based issue, since the control works as expected outside the Kentico CMS. Using the User Control web part should fix this issue.

For more information about the User Control web part please take a look at the Web Parts reference.


Best regards,
Michal Legen