GridView 每行展示复选框,并根据字段内容判断复选框是否可选。用来批量删除数据。
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="jiezhang.aspx.cs" Inherits="jiezhang" StyleSheetTheme="SkinFile" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<style type="text/css">
.auto-style1 {
font-size: x-large;
color: #FF3300;
font-family: 微软雅黑;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div ><center>
<strong><span class="auto-style1">BYF入账记录</span></strong>
<p>
姓名 <asp:TextBox ID="TextBox1" runat="server" Width="250px"></asp:TextBox><br />
待入账 <asp:TextBox ID="TextBox2" runat="server" Width="250px"></asp:TextBox><br />
剩余 <asp:TextBox ID="TextBox3" runat="server" Width="250px"></asp:TextBox><br />
提前入账<asp:TextBox ID="TextBox4" runat="server" Width="250px"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="提交" OnClick="Button1_Click" />
</p>
<asp:GridView ID="GridView1" runat="server" Width="600px" >
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button2" runat="server" Text="删除" OnClick="Button2_Click" /></center> </div>
</form>
</body>
</html>
后台
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class jiezhang : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string sql = "insert into shao_boss_byftj(xm,D1, D2, D3, INDATE, IP) select '" + TextBox1.Text.Trim() + "','" + TextBox2.Text.Trim() + "','" + TextBox3.Text.Trim() + "','" + TextBox4.Text.Trim() + "',sysdate,'" + shaodb.IPAddress() + "' from dual";
string s = shaodb.exesql(sql);
bind();
this.RegisterStartupScript("newWindow", "<script language='javascript'>alert('" + s + "');</script>");
}
void bind()
{
string sql = " select INDATE, D1 待合账, D2 剩余, D3 提前合账, XM 姓名 ,rowid id,IP from shao_boss_byftj where indate>sysdate-8 order by indate";
DataSet ds = shaodb.getds(sql);
GridView1.DataSource = ds;
GridView1.DataBind();
int flag = 0;
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
CheckBox shao = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (GridView1.Rows[i].Cells[7].Text ==shaodb.IPAddress())
{
shao.Enabled = true;
}
else
{
shao.Enabled = false;
}
}
if (GridView1.Rows.Count > 0)
{
Button2.Visible = true;
}
else
{
Button2.Visible = false;
}
}
protected void Button2_Click(object sender, EventArgs e)
{
string vid = "";
int yx = 0;
ArrayList List = new ArrayList();
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
CheckBox shao = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (shao.Checked)
{
vid = GridView1.Rows[i].Cells[6].Text;
yx++;
List.Add("delete from shao_boss_byftj a where rowid='" + vid + "' ");
}
}
if (yx == 0)
{
this.RegisterStartupScript("newWindow", "<script language='javascript'>alert('" + "未选择" + "');</script>");
}
else
{
this.RegisterStartupScript("newWindow", "<script language='javascript'>alert('" + shaodb.batcharry(List) + "');</script>");
List.Clear();
bind();
}
}
}