girdview删除功能的实现

 
< asp:GridView ID = " GridView1 "  runat = " server "  AutoGenerateColumns = " False "  DataSourceID = " AccessDataSource1 "  AllowPaging = " True "  AllowSorting = " True "  DataKeyNames = " id "  Height = " 174px "  Width = " 597px "  OnRowCommand = " GridView1_RowCommand "  OnSelectedIndexChanging = " GridView1_SelectedIndexChanging "  OnRowEditing = " GridView1_RowEditing "  OnSelectedIndexChanged = " GridView1_SelectedIndexChanged "  OnRowDataBound = " GridView1_RowDataBound "  CellPadding = " 4 "  ForeColor = " #333333 "  GridLines = " None "   >
        
< Columns >
            
< asp:TemplateField  HeaderText = " 删除 " >
             
< ItemTemplate >  
< asp:LinkButton   ID = " Button2 "    runat = " server "    CausesValidation = " False "      CommandName = " del "     Text = " 删除 "    CommandArgument = '  <%#   Eval("id")   %>  '      />  
          
</ ItemTemplate >  
            
</ asp:TemplateField >
            
< asp:TemplateField   HeaderText = " 编辑 " >  
        
< ItemTemplate >  
< asp:LinkButton   ID = " Button1 "    runat = " server "    CausesValidation = " False "      CommandName = " edit "     Text = " 编辑 "    CommandArgument = '  <%#   Eval("id")   %>  '      />  
          
</ ItemTemplate >  
          
</ asp:TemplateField >  
            
< asp:BoundField DataField = " title "  HeaderText = " 文章标题 "  SortExpression = " title "   />
            
< asp:BoundField DataField = " Itime "  HeaderText = " 修改时间 "  SortExpression = " Itime "   />
            
< asp:BoundField DataField = " type "  HeaderText = " 类型 "  SortExpression = " type "   />
            
< asp:BoundField DataField = " id "  HeaderText = " 编号 "  InsertVisible = " False "  ReadOnly = " True "
                SortExpression
= " id "   />
           
        
</ Columns >
       
< FooterStyle BackColor = " #5D7B9D "  Font - Bold = " True "  ForeColor = " White "   />
       
< RowStyle BackColor = " #F7F6F3 "  ForeColor = " #333333 "   />
       
< EditRowStyle BackColor = " #999999 "   />
       
< SelectedRowStyle BackColor = " #E2DED6 "  Font - Bold = " True "  ForeColor = " #333333 "   />
       
< PagerStyle BackColor = " #284775 "  ForeColor = " White "  HorizontalAlign = " Center "   />
       
< HeaderStyle BackColor = " #5D7B9D "  Font - Bold = " True "  ForeColor = " White "   />
       
< AlternatingRowStyle BackColor = " White "  ForeColor = " #284775 "   />
    
</ asp:GridView >

在一个模板列中放一个删除的按钮,

<asp:LinkButton   ID="Button2"   runat="server"   CausesValidation="False"     CommandName="del"    Text="删除"   CommandArgument=' <%#   Eval("id")   %> 

这里的commandName很重要,是后代区分功能的参数。CommandArgument是传递操作对象的参数。然后再GridView1_ROwCommand()事件中对不同的操作做处理。但是在实际的应用中好像速度有点。

protected   void  GridView1_RowCommand( object  sender, GridViewCommandEventArgs e)
    
{
        
if(e.CommandName=="edit"
       
{
           
string id = e.CommandArgument.ToString();
           
string url="addnews.aspx?id="+id;
           Response.Redirect(url);
           
//string sql = "delete from news where id='"+id+"'";
           
//OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("law.mdb"));
           
//OleDbCommand myCommand = new OleDbCommand(sql, myConnection);
           
//myCommand.ExecuteNonQuery();
         
       }

       
if (e.CommandName == "del")
       
{
           
string id = e.CommandArgument.ToString();
          
           OleDbConnection MyConnection 
= new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("law.mdb"));
           MyConnection.Open();
           
try
           
{
              
               
string sql = "delete from news where id="+ id ;
               
string sql2 = "select * from news";
               
//OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("law.mdb"));
               OleDbCommand MyCommand = new OleDbCommand(sql, MyConnection);
               MyCommand.ExecuteNonQuery();
               Response.Redirect(
"Managernews.aspx");
               
//OleDbDataAdapter myAdatpter = new OleDbDataAdapter(sql2, MyConnection);
               
//DataSet ds = new DataSet();
               
//myAdatpter.Fill(ds);
               
//GridView1.DataSource = ds;
               
//GridView1.DataBind();
           }

           
catch (Exception ex)
           
{
               Response.Write(ex.Message);
           }

           
finally
           
{
               MyConnection.Close();
           }

           
           

       }
 
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值