asp.net 2.0 中用好delete功能

來源:互聯網
上載者:User

     在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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.