Before marking me down, or flagging as a duplicate I have looked and cannot find my answer on here although there are similar issues - most are relating to javascript calls on asp.net controls. This is not that.
I am trying to provide an id to a checkbox in a repeater based on the value that comes out of the datasource that is bound to the repeater.
I have had no trouble with elements that are not asp.net controls, however when it comes to providing an it to a checkbox like the example below I get the error
Error creating control - rptPayments
The server tag is not well formed.
This is how I am trying to name it at the moment
<asp:CheckBox runat="server" ID="cbValidated_<%# Eval("ContactId")%>" />
I have seen that there are a number of questions out there for similar issues but typically they are for javascript \ OnClientClick(..) funcitons telling people to use single quotes.
if I try single quotes, I get a new error
the ID property of a control can only be set using the ID attribute in the tag and a simple value. Example
I need this as a server side control so that I can loop through all the checkboxes on the click of a button to see which records have been validated or not, but need to link it with the contactId
I assume that your bulk-update button is present outside the repeater. In that case you will have you to loop through the repeater items and proceed like this:-
protected void btnUpdate_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in MyRepeater.Items)
{
CheckBox cbValidated = item.FindControl("cbValidated") as CheckBox;
if (chkTest.Checked)
{
//Do Stuff (You can save some custom values in a list and finally update all)
}
}
}
Update:
As mentioned in the comment the correct way to do this is to store the id of the record is in a hidden variable rather than setting it to the ID
of a control (which is not possible anyways). So simply add one Hidden field like this:-
<ItemTemplate>
<asp:CheckBox ID="cbValidated" runat="server" Text='<%# Eval("foo") %>' />
<asp:HiddenField ID="hdnID" runat="server" Value='<%# Eval("ID") %>' />
</ItemTemplate>
Here, ID
is the name of the column which will map to your record. Then simply find this value if user has checked the checkbox like this:-
//Rest code same as mentioned above
if (chkTest.Checked)
{
HiddenField hdnID = item.FindControl("hdnID") as HiddenField;
string id = hdnID.Value; //use this id for your logic.
}