FindOut_Quran
FindOut_Quran

Reputation: 768

Delete\Update\Insert item in ListView

I have a ListView which has many templates (InsertItemTemplate, AlternatingItemTemplate, EditItemTemplate, EmptyDataTemplate, ItemTemplate, LayoutTemplate, SelectedItemTemplate)

I have this datasource:

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
 ConnectionString="<%$ ConnectionStrings:CouncilsConnectionString %>" 
 SelectCommand="SELECT [EmpID], [EmpName], [UserName], [password], [Email], [Mobile] FROM [Emp] WHERE ([FlageDel] = @FlageDel)"
 DeleteCommand="DELETE FROM Emp WHERE (EmpId = @did)"
 InsertCommand="INSERT INTO Emp(EmpName, UserName, password, Email, Mobile) VALUES (@inm, @ius, @ips, @iem, @imob)"
 UpdateCommand="UPDATE Emp SET EmpName = @ename, UserName = @nm, password = @ps, Email = @em, Mobile = @mob WHERE (EmpId = @eid)">
        <DeleteParameters>
            <asp:Parameter Name="did" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="inm" />
            <asp:Parameter Name="ius" />
            <asp:Parameter Name="ips" />
            <asp:Parameter Name="iem" />
            <asp:Parameter Name="imob" />
        </InsertParameters>
        <SelectParameters>
            <asp:Parameter DefaultValue="0" Name="FlageDel" Type="Int32" />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="ename" />
            <asp:Parameter Name="nm" />
            <asp:Parameter Name="ps" />
            <asp:Parameter Name="em" />
            <asp:Parameter Name="mob" />
            <asp:Parameter Name="eid" />
        </UpdateParameters>
    </asp:SqlDataSource>

And in the ItemTemplate I have

        <td>
            <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" />
            <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Update" />
        </td>

I want to know how can I associate the delete button with the data source command so that it deletes the item when the button is clicked. The same thing for update\insert operations.

Here is the entire ListView Code

<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" InsertItemPosition="LastItem">
        <AlternatingItemTemplate>
            <tr style="background-color: #FFFFFF;color: #000000;">
                <td>
                    <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" CommandArgument="@did" Text="حذف" />
                    <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="تعديل" />
                </td>
                <td>
                    <asp:Label ID="EmpNameLabel" runat="server" Text='<%# Eval("EmpName") %>' />
                </td>
                <td>
                    <asp:Label ID="UserNameLabel" runat="server" Text='<%# Eval("UserName") %>' />
                </td>
                <td>
                    <asp:Label ID="passwordLabel" runat="server" Text='<%# Eval("password") %>' />
                </td>
                <td>
                    <asp:Label ID="EmailLabel" runat="server" Text='<%# Eval("Email") %>' />
                </td>
                <td>
                    <asp:Label ID="MobileLabel" runat="server" Text='<%# Eval("Mobile") %>' />
                </td>
            </tr>
        </AlternatingItemTemplate>
        <EditItemTemplate>
            <tr style="background-color: #999999;">
                <td>
                    <asp:Button ID="UpdateButton" runat="server" CommandName="Update" CommandArgument="@eid" Text="حفظ" />
                    <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="إلغاء" />
                </td>
                <td>
                    <asp:TextBox ID="EmpNameTextBox" runat="server" Text='<%# Bind("EmpName") %>' />
                </td>
                <td>
                    <asp:TextBox ID="UserNameTextBox" runat="server" Text='<%# Bind("UserName") %>' />
                </td>
                <td>
                    <asp:TextBox ID="passwordTextBox" runat="server" Text='<%# Bind("password") %>' />
                </td>
                <td>
                    <asp:TextBox ID="EmailTextBox" runat="server" Text='<%# Bind("Email") %>' />
                </td>
                <td>
                    <asp:TextBox ID="MobileTextBox" runat="server" Text='<%# Bind("Mobile") %>' />
                </td>
            </tr>
        </EditItemTemplate>
        <EmptyDataTemplate>
            <table runat="server" style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;">
                <tr>
                    <td>ليس هناك أي بيانات حاليا.</td>
                </tr>
            </table>
        </EmptyDataTemplate>
        <InsertItemTemplate>
            <tr style="">
                <td>
                    <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="إضافة" />
                    <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="مسح" />
                </td>
                <td>
                    <asp:TextBox ID="EmpNameTextBox" runat="server" Text='<%# Bind("EmpName") %>' />
                </td>
                <td>
                    <asp:TextBox ID="UserNameTextBox" runat="server" Text='<%# Bind("UserName") %>' />
                </td>
                <td>
                    <asp:TextBox ID="passwordTextBox" runat="server" Text='<%# Bind("password") %>' />
                </td>
                <td>
                    <asp:TextBox ID="EmailTextBox" runat="server" Text='<%# Bind("Email") %>' />
                </td>
                <td>
                    <asp:TextBox ID="MobileTextBox" runat="server" Text='<%# Bind("Mobile") %>' />
                </td>
            </tr>
        </InsertItemTemplate>
        <ItemTemplate>
            <tr style="background-color: #EEE;color: black;">
                <td>
                    <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="حذف" />
                    <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="تعديل" />
                </td>
                <td>
                    <asp:Label ID="EmpNameLabel" runat="server" Text='<%# Eval("EmpName") %>' />
                </td>
                <td>
                    <asp:Label ID="UserNameLabel" runat="server" Text='<%# Eval("UserName") %>' />
                </td>
                <td>
                    <asp:Label ID="passwordLabel" runat="server" Text='<%# Eval("password") %>' />
                </td>
                <td>
                    <asp:Label ID="EmailLabel" runat="server" Text='<%# Eval("Email") %>' />
                </td>
                <td>
                    <asp:Label ID="MobileLabel" runat="server" Text='<%# Eval("Mobile") %>' />
                </td>
            </tr>
        </ItemTemplate>
        <LayoutTemplate>
            <table runat="server" style="direction: rtl; width: 100%">
                <tr runat="server">
                    <td runat="server">
                        <table id="itemPlaceholderContainer" runat="server" border="1" style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif; text-align: center; vertical-align: middle">
                            <tr runat="server" style="background-color: #5D7B9D;color: #FFF;">
                                <th runat="server" style="text-align: center"></th>
                                <th runat="server" style="text-align: center">الاسم</th>
                                <th runat="server" style="text-align: center">اسم المستخدم</th>
                                <th runat="server" style="text-align: center">كلمة المرور</th>
                                <th runat="server" style="text-align: center">الايميل</th>
                                <th runat="server" style="text-align: center">الجوال</th>
                            </tr>
                            <tr id="itemPlaceholder" runat="server">
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr runat="server">
                    <td runat="server" style="text-align: center;background-color: #5D7B9D;font-family: Verdana, Arial, Helvetica, sans-serif;color: #FFFFFF">
                        <asp:DataPager ID="DataPager1" runat="server">
                            <Fields>
                                <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowLastPageButton="True" />
                            </Fields>
                        </asp:DataPager>
                    </td>
                </tr>
            </table>
        </LayoutTemplate>
        <SelectedItemTemplate>
            <tr style="background-color: #E2DED6;font-weight: bold;color: #333333;">
                <td>
                    <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="حذف" />
                    <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="تعديل" />
                </td>
                <td>
                    <asp:Label ID="EmpNameLabel" runat="server" Text='<%# Eval("EmpName") %>' />
                </td>
                <td>
                    <asp:Label ID="UserNameLabel" runat="server" Text='<%# Eval("UserName") %>' />
                </td>
                <td>
                    <asp:Label ID="passwordLabel" runat="server" Text='<%# Eval("password") %>' />
                </td>
                <td>
                    <asp:Label ID="EmailLabel" runat="server" Text='<%# Eval("Email") %>' />
                </td>
                <td>
                    <asp:Label ID="MobileLabel" runat="server" Text='<%# Eval("Mobile") %>' />
                </td>
            </tr>
        </SelectedItemTemplate>
    </asp:ListView>

Upvotes: 0

Views: 855

Answers (1)

Caperneoignis
Caperneoignis

Reputation: 1399

Sorry for the delay, but is the delete button not working? generally when you use the templates, it automatically sets it all up so when you click the delete button it runs the deletecommand. However, the Parameters you currently have

<InsertParameters>
        <asp:Parameter Name="inm" />
        <asp:Parameter Name="ius" />
        <asp:Parameter Name="ips" />
        <asp:Parameter Name="iem" />
        <asp:Parameter Name="imob" />
    </InsertParameters>

Don't seem to be really doing any thing, you need to add what they should be expecting. IE string, char, int... something along those lines. The other part, is the command argument that you have added to your buttons. I don't think you need that.

Sorry if this is super late and you already figured it out.

Upvotes: 0

Related Questions