ListView - Delete Row using SqlDataSource.Delete RRS feed

  • Question

  • User260076833 posted


    I have a ListView based on a SqlDataSource:

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

    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);">
            <asp:Parameter Name="idx" Type="Int32"/>

    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?



    Monday, June 29, 2015 6:48 AM