在DataList控件中刪除數據記錄


(一)在DataList控件中刪除記錄的設計要點

DataList控件中刪除數據記錄的設計相對簡單一點。該功能設計的重點在於當用戶單擊【刪除】按鈕時,程序如何判斷要刪除的是哪一行。使DataList控件具有刪除記錄功能的設計要點如下:

●    必須創建ItemTemplate普通項模板,在這個模板內加入一些控件顯示數據源的信息,讓數據管理者快速瀏覽記錄以確定需要刪除哪一條記錄。還需要加入一個“刪除”按鈕到這個模板中,以便能夠啟動刪除功能。

●     將DataList控件的DataKeyField屬性設置到數據表的主鍵字段,以便讓程序知道刪除行的主鍵,正確地刪除對應行。

(二)實現DataList控件中刪除記錄

在這個例子中,使用一個DataList控件,將數據庫MMIS的數據表employeeInfo的信息顯示在ItemTemplate普通項模板中,加入一個“刪除”按鈕到ItemTemplate中啟動刪除功能。

頁面的HTML標記

<form id="Form1" method="post" runat="server">

     <asp:DataList id="DataList1" runat="server"  DataKeyField="編號">

         <HeaderTemplate>刪除員工記錄</HeaderTemplate>

         <ItemTemplate>

              <asp:Button id="Button1" runat="server"

Text="刪除" CommandName="delete">

</asp:Button>&nbsp;編號:

              <asp:Label id=Label1 runat="server"

 Text='<%# DataBinder.Eval(Container.DataItem,"編號") %>'>

              </asp:Label>姓名:

              <asp:Label id=Label2 runat="server"

 Text='<%# DataBinder.Eval(Container.DataItem,"姓名") %>'>

              </asp:Label>性別:

              <asp:Label id=Label3 runat="server"

 Text='<%# DataBinder.Eval(Container.DataItem,"性別") %>'>

              </asp:Label>部門:

              <asp:Label id=Label4 runat="server"

 Text='<%# DataBinder.Eval(Container.DataItem,"部門") %>'>

              </asp:Label>家庭住址:

              <asp:Label id=Label5 runat="server"

 Text='<%# DataBinder.Eval(Container.DataItem,"家庭住址") %>'>

              </asp:Label>

         </ItemTemplate>

     </asp:DataList></FONT>

</form>

 后台代碼

編寫自定義方法MyDataBind連接數據庫、填充所有行到數據集並綁定到控件DataList1上。

private void MyDataBind()

{

     string connectionString="workstation id=localhost;"+

         "initial catalog=MMIS;user id=sa; pwd=";

     SqlConnection myConnection=new SqlConnection(connectionString);

     SqlCommand myCommand=myConnection.CreateCommand();

     myCommand.CommandText="select * from employeeInfo";

     SqlDataAdapter myDataAdapter=new SqlDataAdapter();

     myDataAdapter.SelectCommand=myCommand;

     DataSet mySet=new DataSet();

     //填充數據集          

     myDataAdapter.Fill(mySet,"employeeInfo");

     //數據綁定到控件DataList1

     DataList1.DataSource=mySet.Tables["employeeInfo"].DefaultView;

     DataList1.DataBind();

}

網頁加載時綁定數據。

private void Page_Load(object sender, System.EventArgs e)

{

     if(!IsPostBack)

     {

         //調用自定義函數綁定數據

         MyDataBind();

     }

}

為“刪除”按鈕的單擊編寫程序代碼。因為它為子控件,其CommandName屬性為“delete”,所以程序代碼要寫在容器控件的反升事件DataList1_DeleteCommand中。

下段程序第一行是最關鍵的行,其作用是為了取得被單擊的“刪除”按鈕所在的行的主鍵字段,以便知道要刪除數據庫中的哪一行。在DataList控件的HTML標記中必須要有DataKeyField="主鍵字段"屬性才能使下段程序的第一行有效。本例DataList的標記是:

 <asp:DataList id="DataList1" runat="server"  DataKeyField="編號">

 

private void DataList1_DeleteCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)

{

     //取當前行(就是按下"刪除"按鈕的那一行)的主鍵文本

     string No=DataList1.DataKeys[e.Item.ItemIndex].ToString();

     string connectionString="workstation id=localhost;"+

         "initial catalog=MMIS;user id=sa; pwd=";

     SqlConnection myConnection=new SqlConnection(connectionString);

     SqlCommand myCommand=myConnection.CreateCommand();

     //構建刪除命令

     myCommand.CommandText="delete from employeeInfo where 編號='"+No+"'";

     myConnection.Open();

     //執行刪除

     myCommand.ExecuteNonQuery();

     myConnection.Close();

     MyDataBind();

}

為了慎重起見,在刪除記錄前需要詢問用戶,讓其確認是否真的需要刪除。使用“刪除”按鈕的Attributes.Add方法添加腳本可以做到這一點,但這段代碼不能寫在Page_Load中,因為“刪除”按鈕被加入到了DataList1控件中,成為了DataList1控件的一個子控件,在Page_Load中訪問不到這個按鈕控件。幸好,可以在DataList1控件的ItemCreated事件中實現這個要求,該事件在DataList1控件創建項時發生。

private void DataList1_ItemCreated(object sender,

System.Web.UI.WebControls.DataListItemEventArgs e)

{

     //判斷若是DataList1中的普通項、交替項或者編輯項

     if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==

ListItemType.AlternatingItem||e.Item.ItemType==ListItemType.EditItem)

     {

         //將子控件Button1轉換為ButtonDel

         Button ButtonDel=(Button)e.Item.FindControl("Button1");

         //為"刪除"按鈕添加屬性,以便單擊它時彈出確認框

         ButtonDel.Attributes.Add("onclick","return confirm('確實要刪除此行嗎?');");

     }

}

程序運行結果如圖。

 

    程序運行后,在某行上單擊【刪除】按鈕,彈出一個確認框,在確認框上單擊【取消】,不會做刪除操作;單擊【確定】,那一行被刪除。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM