Question

As part of a sort of photo album I have a datalist that displays a bunch of pictures along with some metadata. This all works nicely. I want to include a check box that flags individual pictures for deletion. Having checked certain boxes, clicking a 'delete' button will fire off a SQL delete command.

Problem is that I have no idea how to identify which check boxes are ticked. I'm thinking of some sort of loop routine that will identify which cb's are ticked and then read the label 'picID1' text value for inclusion in the SQL delete command.

How do I assign unique id's to the cb's and labels?

Here's the markup -

<asp:DataList ID="DataList1" runat="Server" DataSourceID="SqlDataSource1" RepeatColumns="3"
                RepeatDirection="Horizontal" BorderWidth="0px" CellPadding="3">
                <ItemStyle CssClass="item" />
                <ItemTemplate>
                    <table align="left" border="0" cellpadding="0" cellspacing="0">
                        <tr>
                            <td>
                            </td>
                              <td>
                                 <a href='viewphoto_Detailed.aspx?imgID=<%# Eval("imgID") %>'>
                                    <asp:Image ID="picAlbum" runat="server" AlternateText="missing image" ImageUrl='<%# "ShowImage.ashx?id=" & Eval("imgID") %>' Width="300" />
                                 </a>
                              </td>
                            <td>
                            </td>
                        </tr>
                        <tr>
                            <td>
                            </td>
                              <td nowrap="nowrap" width="100" valign="top">
                                <a class="photoData">
                                    Entry# <asp:Label ID="picID1" runat="server" Text='<%#Server.HtmlEncode(Eval("imgID").ToString())%>'></asp:Label>  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  <asp:CheckBox ID="CheckBox1" runat="server" /><br />
                                    Filename submitted: <%#Getname(Server.HtmlEncode(Eval("imgFileNameSubmitted").ToString()))%><br />
                                    Filename saved: <%#Getname(Server.HtmlEncode(Eval("imgFileNameSaved").ToString()))%><br />
                                    Location: <%#Getname(Server.HtmlEncode(Eval("imgWhere").ToString()))%>                                        
                                </a>
                              </td>
                            <td>
                            </td>
                        </tr>
                    </table>
                </ItemTemplate>
            </asp:DataList>
Was it helpful?

Solution

Lots of coffee this morning and a rush of blood to the head and I thought this up. Seems to work ok...

Dim strPicIDs As String = ""
    For Each item As DataListItem In DataList1.Items
        If item.ItemType = ListItemType.Item OrElse item.ItemType = ListItemType.AlternatingItem Then
            Dim DeleteCheckBox As CheckBox = DirectCast(item.FindControl("DeleteCheckBox"), CheckBox)
            Dim picID As Label = DirectCast(item.FindControl("picID"), Label)
            If DeleteCheckBox IsNot Nothing Then
                If DeleteCheckBox.Checked = True Then
                    strPicIDs += picID.Text + ","
                End If
            End If
        End If
    Next
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top