I'm having a bit of an issue trying to get a control in a repeater to maintain state.
I have a custom control (KPIbtn) which is basically a 4 position toggle, that I need to keep value between postbacks.
The value is based on a datatable.
To get this interface up and running I have the KPIbtn created dynamically in a placeholder in a repeater.
This all works fine until I add a another field (StuName) to the repeater. with the extra field on postback I get an error:
Multiple controls with the same ID 'StuName' were found. FindControl requires that controls have unique IDs.
Remove the field - no problems, have the field, but no dynamic control creation, no problems. both - the error appears.
The repeater section of the page looks like...
<asp:Repeater ID="Repeater1" runat="server" OnItemCreated="Repeater1_ItemCreated" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<div>
<asp:Label ID="StuName" runat="server" Text='<%#Eval("StuID") %>'></asp:Label>
<asp:PlaceHolder ID="PlaceHolder2" runat="server"></asp:PlaceHolder>
</div>
</ItemTemplate>
</asp:Repeater>
and the code behind is
protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if (Page.IsPostBack)
{
DTable = ViewState["DTable"] as DataTable;
}
foreach (DataColumn dc in DTable.Columns)
{
KPIbtn btn = new KPIbtn();
btn.ID = dc.ColumnName;
btn.KPIID = dc.ColumnName;
btn.StuID = "test";
PlaceHolder rep = e.Item.FindControl("PlaceHolder2") as PlaceHolder;
rep.Controls.Add(btn);
}
}
Any thoughts on what I am missing or doing wrong? The easy option looks like finding another way of referring to the placeholder without using 'find control', but I fear that at the best that would be glossing over an underlying problems, and there is probably no other option anyway.