程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> 關於ASP.NET >> ASP.NET GridView學習之四 刪除記錄

ASP.NET GridView學習之四 刪除記錄

編輯:關於ASP.NET

將CommandField的ShowDeleteButton=True,那麼當點擊這個CommandField字段時會觸發RowDeleting事件

而BUttonField需要將CommandName=Delete才會激發RowDeleting事件

1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11
12public partial class GridViewDeletingTest : System.Web.UI.Page
13{
14  protected void Page_Load(object sender, EventArgs e)
15  {
16    if (!IsPostBack)
17    {
18      ClientInfoAccessObj accessor = new ClientInfoAccessObj();
19      GridView1.DataSource = accessor.GetAllClients();
20      GridView1.DataBind();
21    }
22  }
23  protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
24  {
25    ClientInfoAccessObj accessor = new ClientInfoAccessObj();
26    int ClientID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);//獲得要刪除的客戶編號
27    accessor.DeleteClientInfoForID(ClientID);//根據客戶ID刪除對應的記錄
28    ClientScript.RegisterClientScriptBlock(this.GetType(), "info", "alert('記錄被刪除');", true);
29    GridView1.DataSource = accessor.GetAllClients();//綁定數據
30    GridView1.DataBind();
31  }
32  protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
33  {
34    //GirdView中的按鈕被點擊之後觸發該事件
35    lblInfo.Text = string.Format("CommandName={0},CommandArgument={1},CommandSource={2}",e.CommandName,e.CommandArgument,e.CommandSource);
36  }
37}
38
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved