Delete\Update\Insert 列表视图中的项目

Delete\Update\Insert item in ListView

我有一个包含许多模板(InsertItemTemplate、AlternatingItemTemplate、EditItemTemplate、EmptyDataTemplate、ItemTemplate、LayoutTemplate、SelectedItemTemplate)的 ListView

我有这个数据源:

<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>

在 ItemTemplate 中我有

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

我想知道如何将删除按钮与数据源命令相关联,以便在单击按钮时删除项目。 update\insert 操作也是如此。

这是整个 ListView 代码

<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>

抱歉耽搁了,是删除按钮不起作用了吗?通常,当您使用模板时,它会自动设置所有内容,因此当您单击删除按钮时,它会运行删除命令。但是,您当前拥有的参数

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

似乎没有真正做任何事情,您需要添加他们应该期待的内容。 IE string, char, int... 类似的东西。 另一部分是您添加到按钮的命令参数。我认为你不需要那个。

抱歉,如果这太晚了,而您已经知道了。