• 在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('确实要删除此行吗?');");

         }

    }

    程序运行结果如图。

     

        程序运行后,在某行上单击【删除】按钮,弹出一个确认框,在确认框上单击【取消】,不会做删除操作;单击【确定】,那一行被删除。

  • 相关阅读:
    C语言指向指针的指针
    C语言注意事项
    C语言指针
    C语言字符串
    C语言数组
    C语言交换两个数的值
    C语言位运算符
    C语言各种进制输出
    C语言中各种进制的表示
    C 语言sizeof运算符
  • 原文地址:https://www.cnblogs.com/losesea/p/2507134.html
Copyright © 2020-2023  润新知