DataGrid控件实现编辑,修改,删除 简单笔记!

前台html代码:
None.gif   < asp:DataGrid  ID ="DataGrid1"  runat ="server"  CellPadding ="4"  ForeColor ="#333333"   AutoGenerateColumns ="False"  Width ="241px"  OnEditCommand ="edit"  OnCancelCommand ="cancel"  OnUpdateCommand ="update"  DataKeyField ="id"  OnDeleteCommand ="DataGrid1_DeleteCommand"  OnItemDataBound ="DataGrid1_ItemDataBound" >
None.gif                
< FooterStyle  BackColor ="#507CD1"  Font-Bold ="True"  ForeColor ="White"   />
None.gif                
< EditItemStyle  BackColor ="#2461BF"   />
None.gif                
< SelectedItemStyle  BackColor ="#D1DDF1"  Font-Bold ="True"  ForeColor ="#333333"   />
None.gif                
< PagerStyle  BackColor ="#2461BF"  ForeColor ="White"  HorizontalAlign ="Center"   />
None.gif                
< AlternatingItemStyle  BackColor ="White"   />
None.gif                
< ItemStyle  BackColor ="#EFF3FB"   />
None.gif                
< HeaderStyle  BackColor ="#507CD1"  Font-Bold ="True"  ForeColor ="White"   />
None.gif                
< Columns >
None.gif                    
< asp:TemplateColumn  HeaderText ="编号" >
None.gif                 
None.gif                        
< ItemTemplate >
None.gif                            
< asp:Label  ID ="id"  runat ="server"  Text ='<%#  DataBinder.Eval(Container.DataItem,"id") % > '> </ asp:Label >
None.gif                        
</ ItemTemplate >
None.gif                    
</ asp:TemplateColumn >
None.gif                    
< asp:TemplateColumn  HeaderText ="场馆" >
None.gif                        
< ItemTemplate >
None.gif                            
< asp:Label  ID ="F_Name"  runat ="server"  Text ='<%#  DataBinder.Eval(Container.DataItem,"Facility_Name") % > '> </ asp:Label >
None.gif                        
</ ItemTemplate >
None.gif                        
< EditItemTemplate >
None.gif                            
< asp:TextBox  ID ="Y_Title"  runat ="server"  Text ='<%#  DataBinder.Eval(Container.DataItem,"Facility_Name") % > '> </ asp:TextBox >
None.gif                        
</ EditItemTemplate >
None.gif                    
</ asp:TemplateColumn >
None.gif                  
< asp:EditCommandColumn  EditText ="编辑"  UpdateText ="更新"  CancelText ="取消"  CausesValidation ="false" >
None.gif                  
</ asp:EditCommandColumn >
None.gif                   
< asp:ButtonColumn  Text ="删除"  CommandName ="Delete"  CausesValidation ="false"   HeaderText ="删除" ></ asp:ButtonColumn >
None.gif                   
None.gif                
</ Columns >
None.gif            
</ asp:DataGrid >
后台cs部分:
 1 None.gif // 编辑
 2 None.gif      protected   void  edit( object  sender,DataGridCommandEventArgs e)
 3 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
 4InBlock.gif        this.DataGrid1.EditItemIndex = e.Item.ItemIndex;
 5InBlock.gif        ChangGuanBind();
 6ExpandedBlockEnd.gif    }

 7 None.gif     // 取消
 8 None.gif      protected   void  cancel( object  sender,DataGridCommandEventArgs e)
 9 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
10InBlock.gif        this.DataGrid1.EditItemIndex = -1;
11InBlock.gif        ChangGuanBind();
12ExpandedBlockEnd.gif    }

13 None.gif     // 编辑更新事件
14 None.gif      protected   void  update( object  sender,DataGridCommandEventArgs e)
15 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
16InBlock.gif       
17InBlock.gif        string sql = "UPDATE YanChu_Facility SET Facility_Name=@Facility_Name WHERE id=@id";
18InBlock.gif        SqlConnection Conn = new SqlConnection(Sqldb);
19InBlock.gif        Conn.Open();
20InBlock.gif        SqlCommand cmd = new SqlCommand(sql,Conn);
21InBlock.gif        cmd.Parameters.Add("@Facility_Name",SqlDbType.NVarChar,50);
22InBlock.gif        cmd.Parameters.Add("@id",SqlDbType.Int,4);
23InBlock.gif        cmd.Parameters["@Facility_Name"].Value = ((TextBox)e.Item.FindControl("Y_Title")).Text;
24InBlock.gif        cmd.Parameters["@id"].Value =this.DataGrid1.DataKeys[e.Item.ItemIndex];
25InBlock.gif        try
26ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
27InBlock.gif            cmd.ExecuteNonQuery();
28InBlock.gif            this.DataGrid1.EditItemIndex = -1;
29InBlock.gif            ChangGuanBind();
30InBlock.gif            Page.ClientScript.RegisterStartupScript(this.GetType(), """alert('修改成功!!');"true);
31InBlock.gif
32ExpandedSubBlockEnd.gif        }

33InBlock.gif        catch(SqlException ex)
34ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
35InBlock.gif            Response.Write(ex.ToString());
36ExpandedSubBlockEnd.gif        }

37InBlock.gif        finally
38ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
39InBlock.gif            Conn.Close();
40InBlock.gif            Conn.Dispose();
41ExpandedSubBlockEnd.gif        }

42ExpandedBlockEnd.gif    }

43 None.gif // 执行删除
44 None.gif      protected   void  DataGrid1_DeleteCommand( object  sender, DataGridCommandEventArgs e)
45 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
46InBlock.gif       
47InBlock.gif               string sqldel = "DELETE FROM YanChu_Facility WHERE id=@id";
48InBlock.gif
49InBlock.gif               SqlConnection Conn = new SqlConnection(Sqldb);
50InBlock.gif               Conn.Open();
51InBlock.gif               SqlCommand Cmd_del = new SqlCommand(sqldel,Conn);
52InBlock.gif               Cmd_del.Parameters.Add("@id",SqlDbType.Int,4);
53InBlock.gif               Cmd_del.Parameters["@id"].Value = this.DataGrid1.DataKeys[e.Item.ItemIndex];
54InBlock.gif               try
55ExpandedSubBlockStart.gifContractedSubBlock.gif               dot.gif{
56InBlock.gif                   Cmd_del.ExecuteNonQuery();
57InBlock.gif                   ChangGuanBind();
58ExpandedSubBlockEnd.gif               }

59InBlock.gif               catch(SqlException ex)
60ExpandedSubBlockStart.gifContractedSubBlock.gif               dot.gif{
61InBlock.gif                   Response.Write("<script>alert('错误!错误内容为-"+ ex.ToString() +"');history.back();</script>");
62ExpandedSubBlockEnd.gif               }

63InBlock.gif               finally
64ExpandedSubBlockStart.gifContractedSubBlock.gif               dot.gif{
65InBlock.gif                   Conn.Close();
66InBlock.gif                   Conn.Dispose();
67InBlock.gif                   
68ExpandedSubBlockEnd.gif               }

69InBlock.gif           
70ExpandedBlockEnd.gif    }

71 None.gif // 删除事件弹出警告
72 None.gif      protected   void  DataGrid1_ItemDataBound( object  sender, DataGridItemEventArgs e)
73 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
74InBlock.gif        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
75ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
76InBlock.gif            e.Item.Attributes.Add("onmouseover""c=this.style.backgroundColor;this.style.backgroundColor='#1e90ff'");
77InBlock.gif            e.Item.Attributes.Add("onmouseout""this.style.backgroundColor=c");
78InBlock.gif           ((LinkButton)(e.Item.Cells[3].Controls[0])).Attributes.Add("onclick""return confirm('确认删除?')");
79InBlock.gif
80ExpandedSubBlockEnd.gif        }

81ExpandedBlockEnd.gif    }

82 None.gif

转载于:https://www.cnblogs.com/mygood/articles/1134640.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值