locked
ListView - Delete Row using SqlDataSource.Delete RRS feed

  • Question

  • User260076833 posted

    Hello,

    I have a ListView based on a SqlDataSource:

    <ListView>
    ...
                   <ItemTemplate>
                        <td align="left" style="" runat="server">
                            <asp:CheckBox runat="server" ID="chk" />
                        </td>
                        <td align="left" style="" runat="server">
                            <asp:Label ID="lbl_Name" runat="server" Text='<%# Eval("name") %>' />
                        </td>
                        <td align="left" style="" runat="server">
                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("nummer") %>' />
                        </td>
                        <td align="left" style="" runat="server">
                            <asp:Label ID="Label2" runat="server" Text='<%# Eval("modus_name") %>' />
                        </td>
                        <td align="left" style="" runat="server">
                            <asp:Label ID="Label3" runat="server" Text='<%# Eval("ausgabe") %>' />
                        </td>
                        <td>
                            <asp:LinkButton ID="btn_Delete" runat="server" CausesValidation="False" CommandName="Delete"
                             Text="Delete Item" OnClick="btn_Delete_Click"></asp:LinkButton>
                        </td>
                    </ItemTemplate>
    </ListView>

    The SqlDataSource has a DeleteCommand with a parameter:

    <asp:SqlDataSource ID="src_Assignment" runat="server" ConnectionString="<%$ ConnectionStrings:StMGP %>"
        SelectCommand="SELECT [idx], [personal], [typ],[name], [nummer],modus,modus_name,ausgabe FROM [assignment] WHERE ([personal] = @idx)"
    ...
        DeleteCommandType="Text" DeleteCommand="DELETE FROM assignment WHERE (idx=@idx);">
    
        <DeleteParameters>
            <asp:Parameter Name="idx" Type="Int32"/>
        </DeleteParameters>
    </asp:SqlDataSource>

    When the user clicks on "Delete Item" in one of the rows, the event handler is called:

           protected void btn_Delete_Click(object sender, EventArgs evt)
            {
               // Here I need to set the "idx" parameter of the DeleteCommand:
               // src_Assignment.DeleteParameters.Add("idx",???)
               // How can I get the idx of the row where the delete button was clicked?
            }
    

    How can I do the following:

    • How can I retrieve the value of the "idx" key for the row where the "Delete Item" button was clicked?
    • How can I set the delete parameter to this value?
    • How do I have to call "src_Assignment.Delete()"? Which parameter do I have to pass to this method?
    • Can I view/trace the resulting SQL command before it gets executed?

    Thanks

    Magnus

    Monday, June 29, 2015 6:48 AM

Answers