在ListView中删除\ Update \ Insert项

时间:2015-01-28 07:53:09

标签: asp.net listview webforms web-controls

我有一个ListView,它有很多模板(InsertItemTemplate,AlternatingItemTemplate,EditItemTemplate,EmptyDataTemplate,ItemTemplate,LayoutTemplate,SelectedItemTemplate)

我有这个数据源:

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

我想知道如何将删除按钮与数据源命令相关联,以便在单击按钮时删除该项目。更新\插入操作也是一样。

这是整个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>

1 个答案:

答案 0 :(得分:0)

抱歉延迟,但是删除按钮无效?通常当您使用模板时,它会自动将其设置为全部,因此当您单击删除按钮时,它会运行delete命令。但是,您当前拥有的参数

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

似乎没有做任何事情,你需要添加他们应该期待的东西。 IE字符串,char,int ......沿着这些行的东西。 另一部分是您添加到按钮的命令参数。我认为你不需要那个。

对不起,如果这是超级晚了你已经弄清楚了。