I found a
real solution. No old-skool HTML, and no compiling of custom DLLs.
A little preface: I assume you (the reader) know how to code-behind, and how that works. Although, this solution will easily work for any non-codebehind file as well.
Here is the gist of it:
You can add any attribute you want to the checkbox control. What we are going to do:
1. When the checkbox control gets populateds from the databind(), we are going to overwrite how that is done, and assign our own custom attribute to the control.
2. When the form does a post-back (submit button, whatever), we are going to read in the custom attribute we assigned to the checkbox control.
My example is done in VB.NET. Here it goes:
First, the function to overwrite the databind function:
Sub rptData_ItemDataBound(source As Object, e As RepeaterItemEventArgs)
If e.Item.ItemType=ListItemType.Item Or e.Item.ItemType=ListItemType.AlternatingItem Then
Dim MyCheckBox As CheckBox = CType(e.Item.FindControl("chkRecord"), CheckBox)
MyCheckBox.Attributes("CustomerID") = e.Item.DataItem("id").ToString()
End If
End Sub
There are a couple things going on there:
We first dynamically search for our checkbox control. In my example, my checkbox control is named "chkRecord". Then, we assign a custom attribute "CustomerID" to that control, and get the ID from the database (In my case, the column I am pulling from the database is "id")
The sub name "rptData_ItemDataBound" is named aptly, because my repeater control is named "rptData" and I am overwriting the "ItemDataBound" event. Note: It really doesn't matter what you name the sub, as you can name it whatever you want.
Now, let's look at how I declare my repeater object (this is a simplified version):
<asp:Repeater ID="rptData" OnItemDataBound="rptData_ItemDataBound" runat="server">
<ItemTemplate>
<asp:CheckBox ID="chkRecord" runat="server" />
</ItemTemplate>
</asp:Repeater>
Notice how I tell it OnItemDataBound to call my custom sub. This overwrites the default method. Now, with that in place, all our check boxes have a custom attribute "CustomerID" that we can reference.
So let's reference it. I have a button called "Submit", that when clicked, is set to call this sub:
Sub btnSubmit_clicked(sender As Object, e As System.EventArgs)
Dim rc AS RepeaterItemCollection = rptData.Items
For Each Item As RepeaterItem In rc
Dim MyCheckBox As CheckBox = CType(Item.FindControl("chkRecord"), CheckBox)
If MyCheckBox.Checked Then HttpContext.Current.Trace.Write("CheckBox Value", MyCheckBox.Attributes("CustomerID"))
Next
End Sub
For each object in the repeater collection, I check to see if it is a check box named "chkRecord", and if it is, then I grab it, and read the custom attribute "CustomerID". Now, I am writing it to the Stack Trace, but you can do whatever you want with it. For example:
strSQL = "DELETE FROM table WHERE ID = " & MyCheckBox.Attributes("CustomerID")