触摸屏租赁
Touch screen rental
广告机租赁
Advertising rental
机器人租赁
Robot leasing
 当前位置:首页 > 全部 > 在DataList状态下删除有选择地删除数据库记录

前台Demo7.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Demo7.aspx.cs" Inherits="Demo7" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center" bgcolor="#c0de98" border="0" cellpadding="0" cellspacing="1" width="99%">
<tr>
<th colspan="6">DataList演示</th>
</tr>
<tr>
<th width="10%">删除</th>
<th width="10%">学生ID</th>
<th width="20%">中文名字</th>
</tr>
<tr>
<td colspan="6" style="width: 100%;" >
<asp:DataList ID="DataList" runat="server" width="100%" DataKeyField="ID" >
<ItemTemplate>
<table align="center" border="0" cellpadding="0" cellspacing="0" width="99%">
<tr>
<td colspan="6" style=" height:4px" mce_style=" height:4px"></td>
</tr>
<tr>
<td width="10%" align="center"><asp:CheckBox ID="CheckBox" runat="server" /></td>   <td width="10%" align="center"><%#DataBinder.Eval(Container.DataItem, "ID")%></td>
<td width="20%" align="center"><%#DataBinder.Eval(Container.DataItem, "area")%></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList></td>
</tr>   
<tr>
<td colspan="6" ><asp:Button ID="Button" runat="server" Text="删 除" CssClass="Button" OnClick="Button_Click" /></td>           
</tr>
</table>
</div>
</form>
</body>
</html>

后台Demo7.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class Demo7 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
         if (Page.IsPostBack == false)
         {
             BindData();
         }
    }
    public void BindData()
     {
         //从配置文件获取数据库连接串
         string strConnection = ConfigurationManager.AppSettings["conStr"];
         SqlConnection conn = new SqlConnection(strConnection);
         conn.Open();
         string sql = "select * from jxc_area order by id desc";//SQL语句
         DataSet ds = new DataSet();

         //使用 SelectCommand 和 SqlConnection 对象初始化 SqlDataAdapter 类的新实例
         SqlDataAdapter da = new SqlDataAdapter(sql, conn);
         da.Fill(ds);
         this.DataList.DataSource = ds;//把表table的数据绑定的到GridView1
         this.DataList.DataBind();//执行绑定
         conn.Close();
     }
    protected void Button_Click(object sender, EventArgs e)
    {
        //从配置文件获取数据库连接串
        string strConnection = ConfigurationManager.AppSettings["conStr"];
        SqlConnection conn = new SqlConnection(strConnection);
        conn.Open();
         foreach (DataListItem CheckBoxItem in DataList.Items)
         {
             if (((CheckBox)CheckBoxItem.FindControl("CheckBox")).Checked)
             {
                 int UserID;
                 UserID = (int)DataList.DataKeys[CheckBoxItem.ItemIndex];
                 string strSql = "Delete  from jxc_area where ID=" + UserID;
                 SqlCommand com = new SqlCommand(strSql, conn);
                 com.ExecuteNonQuery();
             }
         }
         BindData();
    }
}


工作室地址:重庆石桥铺电脑城B座 | 微信:z35544216 | 网址:www.35544216.com