asp.net 2.0 中用好delete功能
点击次数:25 次 发布日期:2008-11-26 10:59:27 作者:源代码网
|
源代码网推荐 觉得十分好,今将要点笔记之,用的都是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 源代码网推荐 源代码网推荐 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
