Hi, I try to create a ItemTemplate for my Repeater with a tree of child controls inside an ItemTemplate. The problem is I can't find a way to persist all my controls. Each time that there is a postback, my controls are lost. I tried many things but I didn't find a way. Is anybody can help me? Here is the code that I use :
Here where I link the ItemTemplate to the repeater
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
RepeaterTemplates qt = new RepeaterTemplates(ListItemType.Item);
repeater.ItemTemplate = qt;
repeater.DataSource = [datasource];
repeater.DataBind();
}
}
Here is the an example of my Itemplate.
public class RepeaterTemplates_Quizzes : ITemplate
{
ListItemType templateType;
public RepeaterTemplates_Quizzes(ListItemType type)
{
templateType = type;
}
public void InstantiateIn(System.Web.UI.Control container)
{
Literal lc = new Literal();
switch(templateType)
{
case ListItemType.Header:
//lc.Text = "<B>" + columnName + "</B>";
//container.Controls.Add(lc);
break;
case ListItemType.Item:
Panel item = new Panel();
item.CssClass = "item";
Panel pnlContent = new Panel();
pnlContent.CssClass = "content";
Panel pnl1 = new Panel();
pnl1.CssClass = "line";
Label lblDateCreatedtitle = new Label();
lblDateCreatedtitle.Text = "Date Created";
lblDateCreatedtitle.CssClass = "datecreatedtitle";
lblDateCreatedtitle.ID = "datecreatedtitle";
pnl1.Controls.Add(lblDateCreatedtitle);
Label lblDateCreated = new Label();
lblDateCreated.DataBinding += new EventHandler(this.BindDataDateCreated);
lblDateCreated.ID = "datecreated";
lblDateCreated.CssClass = "datecreated";
pnl1.Controls.Add(lblDateCreated);
pnlContent.Controls.Add(pnl1);
item.Controls.Add(pnlContent);
container.Controls.Add(item);
break;
case ListItemType.EditItem:
//TextBox tb = new TextBox();
//tb.Text = "";
//container.Controls.Add(tb);
break;
case ListItemType.Footer:
//lc.Text = "<I>" + columnName + "</I>";
//container.Controls.Add(lc);
break;
}
// Create a public method that will handle the
// DataBinding event called in the InstantiateIn method.
public void BindDataDateCreated(object sender, EventArgs e)
{
Label l = (Label)sender;
RepeaterItem container = (RepeaterItem)l.NamingContainer;
l.Text = ((DataRowView)container.DataItem)["DateCreated"].ToString();
}
}
Thank You