1.首先在低三层DAL层与BLL写入删除命令
public static int Delete ( int id)
{
var sql = " delete from Product where id=" + id;
return SqlHelper. NoQuery ( sql) ;
}
2.在首页给删除按钮加入Class属性和data-id属性。(特别强调对于GridView内的删除按钮一定设置class不要哦设置id属性)
我以< a> 标签为例:
< a href= "#" class = "shan" data- id= '<%#Eval("id") %>' > 删除< / a>
3.创建一般处理程序时注意细节问题。
我们以Post为例:
public class Delete : IHttpHandler
{
public void ProcessRequest ( HttpContext context)
{
var id = Convert. ToInt32 ( context. Request. Form[ "data" ] ) ;
int num = ProductManage. Delete ( id) ;
context. Response. ContentType = "application/json" ;
context. Response. Write ( num) ;
}
public bool IsReusable
{
get
{
return false ;
}
}
}
4.script进行操作。
我们已一个简单的例子去写:
< script src= "Scripts/jquery-1.4.1.js" type= "text/javascript" > < / script>
< script type= "text/javascript" >
$( function ( ) {
$( ".shan" ) . live ( "click" ,
function ( ) {
if ( ! confirm ( "确定要删除吗?" ) ) {
return ;
}
var $old = $( this ) ;
var id = $old. attr ( "data-id" ) ;
$. post ( "/Delete.ashx" ,
{ id: id } ,
function ( data) {
if ( data> 0 ) {
alert ( "删除成功!" ) ;
$old. closest ( "tr" ) . remove ( ) ;
}
} ) ;
} ) ;
} )
< / script>
今天小帆的分享就到这里,努力学习天天向上~冲🦆冲🦆