从每行内控制下的listview中删除/删除行

时间:2016-03-14 06:38:57

标签: asp.net vb.net listview

我有listview下有一些数据,在同一个表中我有Button删除特定的行。我想知道如何删除点击它的按钮的行。看下面的样子。

enter image description here

VB代码

 Try
    query = "SELECT * FROM education where userID = '" + userID.Text + "'"
    Dim cmd As New MySqlCommand(query, con)
    cmd.CommandTimeout = 120
    Dim da As New MySqlDataAdapter(cmd)
    Dim table As New DataTable
    da.Fill(table)
    ViewState("Data") = table
    education.DataSource = table
    education.DataBind()
    'countItems.Text = String.Format(table.Rows(0)("dataCount"))
Catch ex As Exception
    Response.Write(ex)
End Try

网页代码

<asp:ListView ID="education" runat="server" DataKeyNames="ID">
    <ItemTemplate>
    <tbody>
        <tr>
            <td class="titleName">
                <asp:Label ID="titleName" runat="server" Text='<%# Eval("degreeName") %>'></asp:Label>
            </td>
            <td class="time">
                <asp:Label ID="university" runat="server" Text='<%# Eval("university") %>'></asp:Label>,
                <asp:Label ID="year" runat="server" Text='<%# Eval("year") %>'></asp:Label>
            </td>
            <td class="actions">
               <asp:HyperLink ID="HyperLink4" ToolTip="Edit" CssClass="editLink" NavigateUrl='<%# "edit-area.aspx?areaID=" & Eval("ID") %>' runat="server"></asp:HyperLink>
              <asp:LinkButton ToolTip="Delete" CssClass="deleteLink" ID="LinkButton4" runat="server"></asp:LinkButton>
           </td>
        </tr>
    </tbody>
  </ItemTemplate>

    <LayoutTemplate>
        <table class="manage-table">
            <thead class="headerMain">
                <tr>
                    <td class="titleName">Degree</td>
                    <td class="time">University</td>
                    <td class="actions">Actions</td>
                </tr>
            </thead>
            <ul id="itemPlaceholderContainer" runat="server" style="">
                <li runat="server" id="itemPlaceholder" />
            </ul>
        </table>
    </LayoutTemplate>
</asp:ListView>

当按钮位于listview下时,如何在后面的代码中找到删除按钮控件。

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作,

在ASPX页面中,

<asp:ImageButton ID="imgDelete" AlternateText="Delete" runat="server" ImageUrl="images/delete.gif"
    CommandName="DeleteItem" CommandArgument='<%# Eval("ID")%>' ToolTip="Delete"
    OnClientClick="return confirm('Are you sure you want to delete this Item?')"
    CausesValidation="false"></asp:ImageButton>

在您的代码隐藏页面中,编写项目命令事件并包含以下代码

If e.CommandName = "DeleteItem" Then
    Dim id As Integer = e.CommandArgument
    Dim itm = (From i In db.Items Where i.ID = id).SingleOrDefault
    If Not itm Is Nothing Then
        db.Items.DeleteOnSubmit(itm)
        db.SubmitChanges()
        bindLab()
        lblMessage.Text = "Item has been deleted successfully"
    End If
End If