我在做留言册的时候,在DataList的Item摸板中,加入一个LinkButton,单击此按钮则删除此条留言!
请问如何实现啊

解决方案 »

  1.   

    void Del_Command(Object sender,DataListCommandEventArgs e)
    {
    SqlConnection con=new SqlConnection(ConfigurationSettings.AppSettings["***"]);
    try
    {
    int id=Convert.ToInt32(shop_guest.DataKeys[(int)e.Item.ItemIndex]);
         con.Open();
         string strSql="DELETE ......";
         SqlCommand cmd=new SqlCommand(strSql,con);
         cmd.ExecuteNonQuery();    
        }
        catch
        {
        }
        finally
        {
         con.Close();
         Binding();
        }
    }<asp:DataList ID="shop_guest" DataKeyField="id" OnDeleteCommand="Del_Command" BorderWidth=0 Width=100% Runat=server RepeatDirection=Vertical BorderColor="#CCCCCC" BorderStyle="Solid">
    <ItemTemplate>
    <table width="80%" border="0" cellspacing="1" cellpadding="3" align="center" bgcolor="#CCCCCC" class="text">
       <tr bgcolor="#FFFFFF"> 
         <td width=10%><%#DataBinder.Eval(Container.DataItem,"id")%></td>
         <td width=65%><%#DataBinder.Eval(Container.DataItem,"content")%></td>
         <td width=20%><%#DataBinder.Eval(Container.DataItem,"datetime")%></td>
         <td width=5% align=center><asp:Button ID="delete" Text="删除" CssClass="border" CommandName="Delete" Runat=server></asp:Button></td>
       </tr>  
    </table>
    </ItemTemplate>
    </asp:DataList>
      

  2.   

    设置LinkButton.CommandName = "Delete";
    编写DataList的OnItemCommand事件处理函数即可!
      

  3.   

    Private Sub DataList1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles DataList1.ItemCommand
            If e.CommandName = "del" Then
                Dim lbl As Label = CType(e.Item.FindControl("label1"), Label)
                Dim conn As New System.Data.OleDb.OleDbConnection("连接字符串")
                conn.Open()
                Dim mycom As New System.Data.OleDb.OleDbCommand("删除语句 where id = " & lbl.Text & "", conn)
                If mycom.ExecuteNonQuery > 0 Then
                    Me.Response.Write("删除成功")
                End If
                conn.Close()
            End If
        End Sub
    label1在前台邦定该记录的ID