In 4guysfromrolla, I saw an article about how to use the delete function in Asp.net 2.0. Article Http://aspnet.4guysfromrolla.com/articles/062007-1.aspx ),
I think it is very good. Now I will take some notes, using the example of northwind.
1) Delete the project in dropdownlist
<Asp: dropdownlist id = "productlist" runat = "server" performanceid = "productsperformancefordropdownlist"
Datatextfield = "productname" datavaluefield = "productid">
</ASP: dropdownlist>
<Asp: sqldatasource id = "productsperformancefordropdownlist" 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"/>
In the delete event, write the followingCode
Protected sub btndeleteproduct_click (byval sender as object, byval e as system. eventargs) handles btndeleteproduct. Click
'Programmatically call the delete Method
Productsperformancefordropdownlist. 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 delete in the gridview
This is a common usage
deletecommand =" delete from [products] Where [productid] = @ productid "
selectcommand =" select [productid], [productname], [unitprice], [Discontinued] from [products] Order by [productname] ">
datakeynames = "productid"
3. deletion is not allowed based on certain conditions. For example, if you want to specify a product with a unit price of <500, you are not allowed to delete it when you click Delete. You need to use the
deleting event
protected sub productsdatasource_deleting (byval sender as object, byval e as system. web. UI. webcontrols. sqldatasourcecommandeventargs) handles productsdatasource. deleting
'termine if the product being deleted has a unit price >$ 50
dim productid as integer = convert. toint32 (E. command. parameters ("@ productid "). value)
'termine the unit price of the product that the user wants to delete...
'there are always 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
note that dim productid as integer = convert. toint32 (E. command. parameters ("@ productid "). value)
and E. cancel = true 'cancel the delete