asp.net 2.0 中用好delete功能

时间:2010-02-24 15:01:21  来源:第二电脑网  作者:第二电脑网

  第二电脑网导读:p:DropDownList> " DeleteCommand="DELETE FROM [Products] WHERE [ProductID] = @ProductID" SelectCommand="SELECT [ProductID], [ProductName] FROM [Products] ORDER BY [ProductName]"> 而在删除的事件中,写入如下代码 Protected Sub btnDeleteProduct_Click(ByVal se...
  正文:在4guysfromrolla中看到老外一篇讲解ASP/ target=_blank class=infotextkey>asp.net 2.0中关于如何用好delete功能的文章(http://aspnet.4guysfromrolla.com/articles/062007-1.aspx), 觉得十分好,今将要点笔记之,用的都是northwind的例子 1)删除dropdownlist中的项目 " DeleteCommand="DELETE FROM [Products] WHERE [ProductID] = @ProductID" SelectCommand="SELECT [ProductID], [ProductName] FROM [Products] ORDER BY [ProductName]"> 而在删除的事件中,写入如下代码 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.RegisterStartuphotoshop/ target=_blank class=infotextkey>pscript(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 这个是很普遍的用法了 " DeleteCommand="DELETE FROM [Products] WHERE [ProductID] = @ProductID" SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice], [Discontinued] FROM [Products] ORDER BY [ProductName]"> 主要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 2.0 中用好delete功能"由第二电脑网原创提供,转载请注明:http://www.002pc.com/master/College/Programming/aspnet/12883.html


关键字:

关于《asp.net 2.0 中用好delete功能》文章的评论

站内搜索: 高级搜索

热门搜索: Windows style 系统 tr IP QQ CPU 安装 function 注册 if td