在DataList控件中删除数据记录
(一)在DataList控件中刪除記錄的設(shè)計(jì)要點(diǎn)
在DataList控件中刪除數(shù)據(jù)記錄的設(shè)計(jì)相對(duì)簡(jiǎn)單一點(diǎn)。該功能設(shè)計(jì)的重點(diǎn)在于當(dāng)用戶單擊【刪除】按鈕時(shí),程序如何判斷要?jiǎng)h除的是哪一行。使DataList控件具有刪除記錄功能的設(shè)計(jì)要點(diǎn)如下:
●????必須創(chuàng)建ItemTemplate普通項(xiàng)模板,在這個(gè)模板內(nèi)加入一些控件顯示數(shù)據(jù)源的信息,讓數(shù)據(jù)管理者快速瀏覽記錄以確定需要?jiǎng)h除哪一條記錄。還需要加入一個(gè)“刪除”按鈕到這個(gè)模板中,以便能夠啟動(dòng)刪除功能。
●?????將DataList控件的DataKeyField屬性設(shè)置到數(shù)據(jù)表的主鍵字段,以便讓程序知道刪除行的主鍵,正確地刪除對(duì)應(yīng)行。
(二)實(shí)現(xiàn)在DataList控件中刪除記錄
在這個(gè)例子中,使用一個(gè)DataList控件,將數(shù)據(jù)庫(kù)MMIS的數(shù)據(jù)表employeeInfo的信息顯示在ItemTemplate普通項(xiàng)模板中,加入一個(gè)“刪除”按鈕到ItemTemplate中啟動(dòng)刪除功能。
頁(yè)面的HTML標(biāo)記
<form id="Form1" method="post" runat="server">
?????<asp:DataList id="DataList1" runat="server"??DataKeyField="編號(hào)">
?????????<HeaderTemplate>刪除員工記錄</HeaderTemplate>
?????????<ItemTemplate>
??????????????<asp:Button id="Button1" runat="server"
Text="刪除" CommandName="delete">
</asp:Button> 編號(hào):
??????????????<asp:Label id=Label1 runat="server"
?Text='<%# DataBinder.Eval(Container.DataItem,"編號(hào)") %>'>
??????????????</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>
?后臺(tái)代碼
編寫自定義方法MyDataBind連接數(shù)據(jù)庫(kù)、填充所有行到數(shù)據(jù)集并綁定到控件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();
?????//填充數(shù)據(jù)集??????????
?????myDataAdapter.Fill(mySet,"employeeInfo");
?????//數(shù)據(jù)綁定到控件DataList1
?????DataList1.DataSource=mySet.Tables["employeeInfo"].DefaultView;
?????DataList1.DataBind();
}
網(wǎng)頁(yè)加載時(shí)綁定數(shù)據(jù)。
private?void?Page_Load(object?sender, System.EventArgs e)
{
?????if(!IsPostBack)
?????{
?????????//調(diào)用自定義函數(shù)綁定數(shù)據(jù)
?????????MyDataBind();
?????}
}
為“刪除”按鈕的單擊編寫程序代碼。因?yàn)樗鼮樽涌丶?#xff0c;其CommandName屬性為“delete”,所以程序代碼要寫在容器控件的反升事件DataList1_DeleteCommand中。
下段程序第一行是最關(guān)鍵的行,其作用是為了取得被單擊的“刪除”按鈕所在的行的主鍵字段,以便知道要?jiǎng)h除數(shù)據(jù)庫(kù)中的哪一行。在DataList控件的HTML標(biāo)記中必須要有DataKeyField="主鍵字段"屬性才能使下段程序的第一行有效。本例DataList的標(biāo)記是:
?<asp:DataList id="DataList1" runat="server"??DataKeyField="編號(hào)">
?
private?void?DataList1_DeleteCommand(object?source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
?????//取當(dāng)前行(就是按下"刪除"按鈕的那一行)的主鍵文本
?????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();
?????//構(gòu)建刪除命令
?????myCommand.CommandText="delete from employeeInfo where 編號(hào)='"+No+"'";
?????myConnection.Open();
?????//執(zhí)行刪除
?????myCommand.ExecuteNonQuery();
?????myConnection.Close();
?????MyDataBind();
}
為了慎重起見,在刪除記錄前需要詢問用戶,讓其確認(rèn)是否真的需要?jiǎng)h除。使用“刪除”按鈕的Attributes.Add方法添加腳本可以做到這一點(diǎn),但這段代碼不能寫在Page_Load中,因?yàn)椤皠h除”按鈕被加入到了DataList1控件中,成為了DataList1控件的一個(gè)子控件,在Page_Load中訪問不到這個(gè)按鈕控件。幸好,可以在DataList1控件的ItemCreated事件中實(shí)現(xiàn)這個(gè)要求,該事件在DataList1控件創(chuàng)建項(xiàng)時(shí)發(fā)生。
private?void?DataList1_ItemCreated(object?sender,
System.Web.UI.WebControls.DataListItemEventArgs e)
{
?????//判斷若是DataList1中的普通項(xiàng)、交替項(xiàng)或者編輯項(xiàng)
?????if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==
ListItemType.AlternatingItem||e.Item.ItemType==ListItemType.EditItem)
?????{
?????????//將子控件Button1轉(zhuǎn)換為ButtonDel
?????????Button ButtonDel=(Button)e.Item.FindControl("Button1");
?????????//為"刪除"按鈕添加屬性,以便單擊它時(shí)彈出確認(rèn)框
?????????ButtonDel.Attributes.Add("onclick","return confirm('確實(shí)要?jiǎng)h除此行嗎?');");
?????}
}
程序運(yùn)行結(jié)果如圖。
?
轉(zhuǎn)載于:https://www.cnblogs.com/dotnetfangjun/archive/2012/03/30/2425202.html
總結(jié)
以上是生活随笔為你收集整理的在DataList控件中删除数据记录的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信小程序上传大于4m_微信定制小程序开
- 下一篇: hooks组件封装 react_名符其实