<
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"
>
< FooterStyle BackColor ="#507CD1" Font-Bold ="True" ForeColor ="White" />
< EditItemStyle BackColor ="#2461BF" />
< SelectedItemStyle BackColor ="#D1DDF1" Font-Bold ="True" ForeColor ="#333333" />
< PagerStyle BackColor ="#2461BF" ForeColor ="White" HorizontalAlign ="Center" />
< AlternatingItemStyle BackColor ="White" />
< ItemStyle BackColor ="#EFF3FB" />
< HeaderStyle BackColor ="#507CD1" Font-Bold ="True" ForeColor ="White" />
< Columns >
< asp:TemplateColumn HeaderText ="编号" >
< ItemTemplate >
< asp:Label ID ="id" runat ="server" Text ='<%# DataBinder.Eval(Container.DataItem,"id") % > '> </ asp:Label >
</ ItemTemplate >
</ asp:TemplateColumn >
< asp:TemplateColumn HeaderText ="场馆" >
< ItemTemplate >
< asp:Label ID ="F_Name" runat ="server" Text ='<%# DataBinder.Eval(Container.DataItem,"Facility_Name") % > '> </ asp:Label >
</ ItemTemplate >
< EditItemTemplate >
< asp:TextBox ID ="Y_Title" runat ="server" Text ='<%# DataBinder.Eval(Container.DataItem,"Facility_Name") % > '> </ asp:TextBox >
</ EditItemTemplate >
</ asp:TemplateColumn >
< asp:EditCommandColumn EditText ="编辑" UpdateText ="更新" CancelText ="取消" CausesValidation ="false" >
</ asp:EditCommandColumn >
< asp:ButtonColumn Text ="删除" CommandName ="Delete" CausesValidation ="false" HeaderText ="删除" ></ asp:ButtonColumn >
</ Columns >
</ asp:DataGrid >
后台cs部分:
< FooterStyle BackColor ="#507CD1" Font-Bold ="True" ForeColor ="White" />
< EditItemStyle BackColor ="#2461BF" />
< SelectedItemStyle BackColor ="#D1DDF1" Font-Bold ="True" ForeColor ="#333333" />
< PagerStyle BackColor ="#2461BF" ForeColor ="White" HorizontalAlign ="Center" />
< AlternatingItemStyle BackColor ="White" />
< ItemStyle BackColor ="#EFF3FB" />
< HeaderStyle BackColor ="#507CD1" Font-Bold ="True" ForeColor ="White" />
< Columns >
< asp:TemplateColumn HeaderText ="编号" >
< ItemTemplate >
< asp:Label ID ="id" runat ="server" Text ='<%# DataBinder.Eval(Container.DataItem,"id") % > '> </ asp:Label >
</ ItemTemplate >
</ asp:TemplateColumn >
< asp:TemplateColumn HeaderText ="场馆" >
< ItemTemplate >
< asp:Label ID ="F_Name" runat ="server" Text ='<%# DataBinder.Eval(Container.DataItem,"Facility_Name") % > '> </ asp:Label >
</ ItemTemplate >
< EditItemTemplate >
< asp:TextBox ID ="Y_Title" runat ="server" Text ='<%# DataBinder.Eval(Container.DataItem,"Facility_Name") % > '> </ asp:TextBox >
</ EditItemTemplate >
</ asp:TemplateColumn >
< asp:EditCommandColumn EditText ="编辑" UpdateText ="更新" CancelText ="取消" CausesValidation ="false" >
</ asp:EditCommandColumn >
< asp:ButtonColumn Text ="删除" CommandName ="Delete" CausesValidation ="false" HeaderText ="删除" ></ asp:ButtonColumn >
</ Columns >
</ asp:DataGrid >
1
//
编辑
2 protected void edit( object sender,DataGridCommandEventArgs e)
3 {
4 this.DataGrid1.EditItemIndex = e.Item.ItemIndex;
5 ChangGuanBind();
6 }
7 // 取消
8 protected void cancel( object sender,DataGridCommandEventArgs e)
9 {
10 this.DataGrid1.EditItemIndex = -1;
11 ChangGuanBind();
12 }
13 // 编辑更新事件
14 protected void update( object sender,DataGridCommandEventArgs e)
15 {
16
17 string sql = "UPDATE YanChu_Facility SET Facility_Name=@Facility_Name WHERE id=@id";
18 SqlConnection Conn = new SqlConnection(Sqldb);
19 Conn.Open();
20 SqlCommand cmd = new SqlCommand(sql,Conn);
21 cmd.Parameters.Add("@Facility_Name",SqlDbType.NVarChar,50);
22 cmd.Parameters.Add("@id",SqlDbType.Int,4);
23 cmd.Parameters["@Facility_Name"].Value = ((TextBox)e.Item.FindControl("Y_Title")).Text;
24 cmd.Parameters["@id"].Value =this.DataGrid1.DataKeys[e.Item.ItemIndex];
25 try
26 {
27 cmd.ExecuteNonQuery();
28 this.DataGrid1.EditItemIndex = -1;
29 ChangGuanBind();
30 Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('修改成功!!');", true);
31
32 }
33 catch(SqlException ex)
34 {
35 Response.Write(ex.ToString());
36 }
37 finally
38 {
39 Conn.Close();
40 Conn.Dispose();
41 }
42 }
43 // 执行删除
44 protected void DataGrid1_DeleteCommand( object sender, DataGridCommandEventArgs e)
45 {
46
47 string sqldel = "DELETE FROM YanChu_Facility WHERE id=@id";
48
49 SqlConnection Conn = new SqlConnection(Sqldb);
50 Conn.Open();
51 SqlCommand Cmd_del = new SqlCommand(sqldel,Conn);
52 Cmd_del.Parameters.Add("@id",SqlDbType.Int,4);
53 Cmd_del.Parameters["@id"].Value = this.DataGrid1.DataKeys[e.Item.ItemIndex];
54 try
55 {
56 Cmd_del.ExecuteNonQuery();
57 ChangGuanBind();
58 }
59 catch(SqlException ex)
60 {
61 Response.Write("<script>alert('错误!错误内容为-"+ ex.ToString() +"');history.back();</script>");
62 }
63 finally
64 {
65 Conn.Close();
66 Conn.Dispose();
67
68 }
69
70 }
71 // 删除事件弹出警告
72 protected void DataGrid1_ItemDataBound( object sender, DataGridItemEventArgs e)
73 {
74 if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
75 {
76 e.Item.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#1e90ff'");
77 e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
78 ((LinkButton)(e.Item.Cells[3].Controls[0])).Attributes.Add("onclick", "return confirm('确认删除?')");
79
80 }
81 }
82
2 protected void edit( object sender,DataGridCommandEventArgs e)
3 {
4 this.DataGrid1.EditItemIndex = e.Item.ItemIndex;
5 ChangGuanBind();
6 }
7 // 取消
8 protected void cancel( object sender,DataGridCommandEventArgs e)
9 {
10 this.DataGrid1.EditItemIndex = -1;
11 ChangGuanBind();
12 }
13 // 编辑更新事件
14 protected void update( object sender,DataGridCommandEventArgs e)
15 {
16
17 string sql = "UPDATE YanChu_Facility SET Facility_Name=@Facility_Name WHERE id=@id";
18 SqlConnection Conn = new SqlConnection(Sqldb);
19 Conn.Open();
20 SqlCommand cmd = new SqlCommand(sql,Conn);
21 cmd.Parameters.Add("@Facility_Name",SqlDbType.NVarChar,50);
22 cmd.Parameters.Add("@id",SqlDbType.Int,4);
23 cmd.Parameters["@Facility_Name"].Value = ((TextBox)e.Item.FindControl("Y_Title")).Text;
24 cmd.Parameters["@id"].Value =this.DataGrid1.DataKeys[e.Item.ItemIndex];
25 try
26 {
27 cmd.ExecuteNonQuery();
28 this.DataGrid1.EditItemIndex = -1;
29 ChangGuanBind();
30 Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('修改成功!!');", true);
31
32 }
33 catch(SqlException ex)
34 {
35 Response.Write(ex.ToString());
36 }
37 finally
38 {
39 Conn.Close();
40 Conn.Dispose();
41 }
42 }
43 // 执行删除
44 protected void DataGrid1_DeleteCommand( object sender, DataGridCommandEventArgs e)
45 {
46
47 string sqldel = "DELETE FROM YanChu_Facility WHERE id=@id";
48
49 SqlConnection Conn = new SqlConnection(Sqldb);
50 Conn.Open();
51 SqlCommand Cmd_del = new SqlCommand(sqldel,Conn);
52 Cmd_del.Parameters.Add("@id",SqlDbType.Int,4);
53 Cmd_del.Parameters["@id"].Value = this.DataGrid1.DataKeys[e.Item.ItemIndex];
54 try
55 {
56 Cmd_del.ExecuteNonQuery();
57 ChangGuanBind();
58 }
59 catch(SqlException ex)
60 {
61 Response.Write("<script>alert('错误!错误内容为-"+ ex.ToString() +"');history.back();</script>");
62 }
63 finally
64 {
65 Conn.Close();
66 Conn.Dispose();
67
68 }
69
70 }
71 // 删除事件弹出警告
72 protected void DataGrid1_ItemDataBound( object sender, DataGridItemEventArgs e)
73 {
74 if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
75 {
76 e.Item.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#1e90ff'");
77 e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
78 ((LinkButton)(e.Item.Cells[3].Controls[0])).Attributes.Add("onclick", "return confirm('确认删除?')");
79
80 }
81 }
82