Handling autopostbacks within a repeater - Checkbox Style

An issue I had today stumped me for awhile. I had a repeater, and inside it I had a checkbox. When the checkbox was updated (checked / unchecked) I needed it to postback to the server. I found a solution.  Here's my Repeater layout:

<asp:Repeater ID="rptApprovers" runat="server">

    <HeaderTemplate><table></HeaderTemplate>

    <ItemTemplate>

        <tr>

            <td style="width:180px;">

                <asp:Literal ID="litStaffID" Text='<%#DataBinder.Eval(Container.DataItem,"StaffID") %>' runat="server" />

                <asp:Literal ID="litStaffName" Text='<%#DataBinder.Eval(Container.DataItem,"FullName") %>' runat="server" />

            </td>

            <td style="width:100px;">

                <asp:CheckBox ID="chkApproved"  Checked='<%#  Boolean.Parse(DataBinder.Eval(Container.DataItem,"Approved").ToString()) %>' OnCheckedChanged="chkApproved_CheckChanged" Text="Approved" runat="server" AutoPostBack="true" />

            </td>

        </tr>

    </ItemTemplate>

    <FooterTemplate></table></FooterTemplate>

</asp:Repeater>

What I needed was when clicking a checkbox, was for it to postback and update my Database.
Seeing the repeater does not have a way of tracking autopostbacks that happen from controls within the repeater, I had to do this within the checkboxes OnCheckChanged event:
 

To accomplish this I needed to do the following code: 
CheckBox chkBox = (CheckBox)sender;		
RepeaterItem riContainer = (RepeaterItem)chkBox.NamingContainer;		
int intStaffID;		
//Get the StaffID to update		
intStaffID = Int32.Parse(((Literal)riContainer.FindControl("litStaffID")).Text);
//Update the DataBase
 
I've never used the NamingContainer property before, and in this case it has turned out to be quite helpful.

No Comments