Sep 19 2006

Deleting files, checkboxes in repeater - howto part 5

Posted by admin under In practice

It is highly recommended you start this tutorial article serie from the beginning.

In this part we will continue our work with the upload and file management. In part 4 we created Edit customer form with three upload controls - but also we have a repeater section showing all existing files.

So, we are trying to create a "multipurpose" New/Edit form, where the user in one single form can do all modifications at once, i.e change name to "Kalle" AND remove one of the existing files AND upload a new file (excactly as we see in the picture above).

Simple editing and uploading is already fixed in our project - now lets do the delete thing.

First we create the stored procedure:



CREATE PROCEDURE cust_DeleteFile(@id int) AS
delete from cust_file where id=@id
GO


When clicking the save button we now need to add functionality to loop through the existing files in the repeater - and delete the files where the checkbox is checked. How are we gonna do that? Lets start with the repeater:



   Existing files:<br />
   <asp:Repeater ID="rptExistingFiles" runat="server" OnItemDataBound="rptExistingFiles_ItemDataBound">
   <HeaderTemplate>
   <table border="1">
   <tr>
   <td>
   Delete
   </td>
   <td>
   Name
   </td>
   </tr>
   </HeaderTemplate>
   <ItemTemplate>
   <tr>
   <td>
   <asp:CheckBox ID="chkBoxDelete" runat="server" />
   </td>
   <td><asp:HyperLink ID="hlGetFile" runat="server"></asp:HyperLink></td>
   </tr>
   </ItemTemplate>
   <FooterTemplate>
   </table>
   </FooterTemplate>
   </asp:Repeater>


The secret is to (dynamically in our ItemDataBound function) add the unique id for each file into an html element attribute:



    protected void rptExistingFiles_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DataRowView row = e.Item.DataItem as DataRowView;

'''   old code not showed here for clarity
'''
            CheckBox chkBoxDelete = e.Item.FindControl("chkBoxDelete") as CheckBox;
            chkBoxDelete.Attributes.Add("internalid", row["id"].ToString());

        }
    }


ASP.NET will find this added attribute and create a surrounding span <span internalid="4"></span> for each repeater item (i.e for each existing file). And the good thing it's so easy to retrieve it in our Save function:



            //Might delete some files as well?
            foreach (RepeaterItem oItem in rptExistingFiles.Items)
            {
                CheckBox chkBoxDelete = oItem.FindControl("chkBoxDelete") as CheckBox;
                if (chkBoxDelete.Checked)
                {
                    int nFileId = Convert.ToInt32(chkBoxDelete.Attributes["internalid"]);
                    //delete
                    SqlParameter[] paramsToSP2 = new SqlParameter[1];
                    paramsToSP2[0] = new SqlParameter("@id", SqlDbType.Int);
                    paramsToSP2[0].Value = nFileId;

                    Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteNonQuery(ConfigurationManager.ConnectionStrings["MainConn"].ToString(),
                        CommandType.StoredProcedure, "cust_DeleteFile", paramsToSP2);
                }
            }