复选框删除多条数据

接口:

     int findDeleteAll(String [] trainNo);

Biz:

    @Override
        public int updateByid(Tstrain tstrain) {
            int result=    tstrainDao.updateByid(tstrain);
            return result;
        }

Dao:

    //多个删除
    @Override
    public int findDeleteAll(String []trainNo) {
        StringBuffer sb=new StringBuffer("DELETE from train_info WHERE train_no IN(");//创建StudentBuffer
        int result=0;
        //解决方案1 循环调用弊端(频繁连接数据库)中裤sql报错 无法回滚已经执行的sql语句
        if(trainNo!=null&&trainNo.length>0) {
        for(int i=0;i<trainNo.length;i++) {
            sb.append("'"+trainNo[i]+"'");//循环一次添加一次
            if(i!=trainNo.length-1) {//如果是最后一个追加一个 不是-一个,s
                sb.append(",");//追加
            }
        }
        sb.append(")");
        result=super.executeUpdate(sb.toString());
        }
        return result;
        
    }

首页:

<%@page import="com.haochenit.entity.Tstrain"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%
   List<Tstrain>tstrainlist=(List<Tstrain>)request.getAttribute("Tstrainlist");
        if(tstrainlist==null){
            response.sendRedirect("do_list.jsp");
            return;
        }
    %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>火车列表</title>
 <script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
 <script type="text/javascript">
    function del() {
        $("#dent").submit();
}
 </script>
</head>
<body>
             <form id="dent"  method="post" action="do_del.jsp" >
    <table border="1" align="center">
    <tr>
    <td><a href="javascript:del();" >删除</a></td>
    <td><a>车次</a></td>
    <td>发车---到达</td>
    <td>发时---到时</td>
    <td>车型</td>
    <td>运行时长(小时)</td>
    <td>操作</td>
    </tr>
    <%
    for(Tstrain tstrain:tstrainlist){
        %>
        <tr>
        <td><input type="checkbox" name="id"value="<%=tstrain.getTrainNo() %>"/></td>
        <td><a href="info.jsp?id=<%=tstrain.getTrainNo() %>"><%=tstrain.getTrainNo() %>    </a></td>
        <td><%=tstrain.getTrainStation() %>---<%=tstrain.getArrivalStation() %></td>
        <td><%=tstrain.getStartTime() %>----<%=tstrain.getArrivalTime() %></td>
        <td><%=tstrain.getVehiclemodel() %></td>
        <td><%=tstrain.getRuntime()  %></td>
        <td><a href="update.jsp?id=<%=tstrain.getTrainNo() %>">修改</a></td>
        <td><a href="">新增</a></td>
        </tr>
    <%
    }
    %>
    </table>
 </form>
</body>
</html>

删除处理页面:

<%@page import="com.haochenit.dao.impl.TstrainDaoImpl"%>
<%@page import="com.haochenit.dao.TstrainDao"%>
<%@page import="com.haochenit.entity.Tstrain"%>
<%@page import="com.haochenit.biz.impl.TstrainBizDaoImpl"%>
<%@page import="com.haochenit.biz.TstrainBizDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
            TstrainBizDao tstrainBizDao=new TstrainBizDaoImpl();
        //获取多个id值   使用数组 接受所有id值
            String ids[]=request.getParameterValues("id");
            
        int result=    tstrainBizDao.findDeleteAll(ids);
        out.print("<script>");
            if(result>0){
                out.print("alert('删除成功');");
    }    else{
                out.print("alert('删除失败');");
    }    
            out.print("loaction.href='list.jsp'");
            out.print("</script>");
%>

要实现这个功能,你需要按照以下步骤: 1. 在GridView中添加一个列来显示checkbox。 2. 在你的C#代码中,为GridView的RowDataBound事件添加一个事件处理程序,来设置checkbox的初始状态。 3. 当用户勾选或取消某个checkbox时,在GridView的RowCommand事件处理程序中,根据checkbox的状态来动态添加或删除相应的y轴。 以下是具体的代码实现: 1. 在GridView中添加一个列来显示checkbox。 ```aspx <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound" OnRowCommand="GridView1_RowCommand"> <Columns> <asp:TemplateField> <HeaderTemplate> <asp:CheckBox ID="chkSelectAll" runat="server" OnClick="CheckAllCheckBoxes(this);" /> </HeaderTemplate> <ItemTemplate> <asp:CheckBox ID="chk" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="x" HeaderText="x轴" /> <asp:BoundField DataField="y1" HeaderText="y轴1" /> <asp:BoundField DataField="y2" HeaderText="y轴2" /> </Columns> </asp:GridView> ``` 2. 在你的C#代码中,为GridView的RowDataBound事件添加一个事件处理程序,来设置checkbox的初始状态。 ```csharp protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { CheckBox chk = (CheckBox)e.Row.FindControl("chk"); if (chk != null) { chk.Checked = false; // 初始状态为未勾选 } } } ``` 3. 当用户勾选或取消某个checkbox时,在GridView的RowCommand事件处理程序中,根据checkbox的状态来动态添加或删除相应的y轴。 ```csharp protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Select") { int index = Convert.ToInt32(e.CommandArgument); GridViewRow row = GridView1.Rows[index]; CheckBox chk = (CheckBox)row.FindControl("chk"); if (chk.Checked) { // 添加一条y轴 string yFieldName = "y" + (GridView1.Columns.Count - 1); Series series = new Series(yFieldName); Chart1.Series.Add(series); series.ChartArea = "ChartArea1"; series.ChartType = SeriesChartType.Line; series.BorderWidth = 2; series.Color = Color.Blue; series.YValueMembers = yFieldName; series.XValueMember = "x"; } else { // 删除一条y轴 string yFieldName = "y" + (GridView1.Columns.Count - 2); Series series = Chart1.Series[yFieldName]; Chart1.Series.Remove(series); } } } ``` 注意: 1. 以上代码中的Chart1是一个ASP.NET Chart控件,你需要在页面中添加一个Chart控件,并设置好它的数据源。 2. 以上代码中的CheckAllCheckBoxes函数是一个JavaScript函数,用来实现全选/取消全选功能,你需要在页面中添加这个函数。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值