asp.net 2.0 中用好delete功能

     在4guysfromrolla中看到老外一篇讲解asp.net 2.0中关于如何用好delete功能的文章(http://aspnet.4guysfromrolla.com/articles/062007-1.aspx),
觉得十分好,今将要点笔记之,用的都是northwind的例子
  
1)删除dropdownlist中的项目
                 <asp:DropDownList ID="ProductList" runat="server" DataSourceID="ProductsDataSourceForDropDownList"
            DataTextField="ProductName" DataValueField="ProductID">
        </asp:DropDownList>
       
        <asp:SqlDataSource ID="ProductsDataSourceForDropDownList" runat="server"
            ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" DeleteCommand="DELETE FROM [Products] WHERE [ProductID] = @ProductID"
            SelectCommand="SELECT [ProductID], [ProductName] FROM [Products] ORDER BY [ProductName]">
            <DeleteParameters>
                <asp:ControlParameter ControlID="ProductList" Name="ProductID" PropertyName="SelectedValue"
                    Type="Int32" />
            </DeleteParameters>
        </asp:SqlDataSource>
     <asp:Button ID="btnDeleteProduct" runat="server" Text="Delete Selected Product" />

     而在删除的事件中,写入如下代码
 Protected Sub btnDeleteProduct_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDeleteProduct.Click
        'Programmatically call the Delete method
        ProductsDataSourceForDropDownList.Delete()

               Dim productJustDeleted As String = ProductList.SelectedItem.Text
        ClientScript.RegisterStartupScript(Me.GetType(), "Message", String.Format("alert('The product {0} has just been deleted...');", productJustDeleted.Replace("'", "\'")), True)

        'Rebind the data to the DropDownList so that the just-deleted product no longer appears
        ProductList.DataBind()
    End Sub


2  gridview中的delete
   这个是很普遍的用法了
   <asp:SqlDataSource ID="ProductsDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            DeleteCommand="DELETE FROM [Products] WHERE [ProductID] = @ProductID"
            SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice], [Discontinued] FROM [Products] ORDER BY [ProductName]">
            <DeleteParameters>
                <asp:Parameter Name="ProductID" Type="Int32" />
            </DeleteParameters>
        </asp:SqlDataSource>

主要gridview中要设置 好 DataKeyNames="ProductID"
   
3  根据一定的条件不允许删除。比如要规定,单价<500的商品,用户在点删除时不允许删除,这个时候要用到
deleting 事件
Protected Sub ProductsDataSource_Deleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles ProductsDataSource.Deleting
        'Determine if the product being deleted has a unit price > $50
        Dim productID As Integer = Convert.ToInt32(e.Command.Parameters("@ProductID").Value)

        'Determine the unit price of the product that the user wants to delete...
        'There are many ways you can accomplish this - use a SqlDataSource control, write code to query the database...
        'Let's use data access code
        Dim myConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("NorthwindConnectionString").ConnectionString)
        Const strSql As String = "SELECT UnitPrice FROM Products WHERE ProductID = @ProductID"
        Dim myCommand As New SqlCommand(strSql, myConnection)
        myCommand.Parameters.AddWithValue("@ProductID", productID)

        myConnection.Open()
        Dim price As Object = myCommand.ExecuteScalar
        myConnection.Close()

        'Prohibit the delete if price is not a database NULL AND it is greater than $50
        If Not Convert.IsDBNull(price) AndAlso Convert.ToDecimal(price) > 50 Then
            e.Cancel = True     'Cancel the delete
            CannotDeleteMessage.Visible = True  'Show a message explaining why
        End If
    End Sub
   
要注意其中     Dim productID As Integer = Convert.ToInt32(e.Command.Parameters("@ProductID").Value)
和    e.Cancel = True     'Cancel the delete

你可能感兴趣的:(asp.net)