Problem is, even the placeholder control is dynamically created so I can't
reference it in the code. Any thoughts?
- Show quoted text -
Hi,
In this case first of all you need to recreate ALL the controls in
postback.
Here is a piece of code I use to create and fill a repeater in code.
There are some classes of mine that I use (cause I bind to a
collection of business objetcs) that yo will not have, but you will
get the idea of how it is done.
List<Control> CreateRepeaterItemsControls() {
List<Control> controlsInRow = new List<Control>();
controlsInRow.Add(new LiteralControl("<tr class=
\"RoleControlRepeaterRowCssClass\"><td align=\"right\" style=\"width:
50%\">"));
Control c = new Label();
c.ID = "RoleNameLBL";
controlsInRow.Add(c);
controlsInRow.Add(new LiteralControl("</td><td align=\"left
\" style=\"width: 50%\">"));
c = new CheckBox();
c.ID = "RoleStatusCHK";
controlsInRow.Add(c);
spanDiv = new HtmlGenericControl();
spanDiv.ID = "SpanDiv";
spanDiv.Style.Add("visibility", "hidden");
messageLBL = new Label();
messageLBL.ID = "MessageLBL";
spanDiv.Controls.Add(messageLBL);
controlsInRow.Add(spanDiv);
controlsInRow.Add(new LiteralControl("</td><td></td></
tr>"));
return controlsInRow;
}
List<Control> CreateRepeaterHeaderControls() {
List<Control> controlsInRow = new List<Control>();
controlsInRow.Add(new LiteralControl("<table width=\"100%
\">"));
return controlsInRow;
}
List<Control> CreateRepeaterFooterControls() {
List<Control> controlsInRow = new List<Control>();
controlsInRow.Add(new LiteralControl("</table>"));
return controlsInRow;
}
//The delegate declaration for the above methods
delegate List<Control> RepeaterControlCreator();
//This is the class that implement the template for each row
of the repeater
class RepeaterTemplate: ITemplate {
RepeaterControlCreator controlCreator;
public RepeaterTemplate(RepeaterControlCreator
controlCreator) {
this.controlCreator = controlCreator;
}
public void InstantiateIn(System.Web.UI.Control container)
{
foreach (Control c in controlCreator())
container.Controls.Add(c);
}
}
protected override void CreateChildControls() {
base.CreateChildControls();
roleLST = new Repeater();
roleLST.ItemDataBound += new
RepeaterItemEventHandler(RoleLST_ItemDataBound);
roleLST.HeaderTemplate = new
RepeaterTemplate( new
RepeaterControlCreator(CreateRepeaterHeaderControls)) ;
roleLST.ItemTemplate = new
RepeaterTemplate( new
RepeaterControlCreator(CreateRepeaterItemsControls));
roleLST.AlternatingItemTemplate = new
RepeaterTemplate( new
RepeaterControlCreator(CreateRepeaterItemsControls));
roleLST.FooterTemplate = new
RepeaterTemplate( new
RepeaterControlCreator(CreateRepeaterFooterControls));
Controsl.Add( roleLST);
}
protected void OkBTN_Click(object sender, EventArgs e) {
//update the status
int index = 0;
foreach (RepeaterItem rItem in roleLST.Items) {
CheckBox cb = rItem.FindControl("RoleStatusCHK") as
CheckBox;
}