首页
最近更新
热门教程
ASP.NET
开发语言
开发环境
AJax教程
控件开发
统计报表
数据库
Web服务
安装部署
HTML教程
Javascript
XML教程
Community Server
NHibernate
书籍推荐
常用工具
实用代码
教程全文搜索
首页
>>
ASP.NET
GridView 72般绝技(二)
添加人:
admin
添加时间:2007-08-11 阅读次数:5298
收藏此教程
5.GridView和CheckBox结合:
效果图:
后台代码:
1
using
System;
2
using
System.Data;
3
using
System.Configuration;
4
using
System.Web;
5
using
System.Web.Security;
6
using
System.Web.UI;
7
using
System.Web.UI.WebControls;
8
using
System.Web.UI.WebControls.WebParts;
9
using
System.Web.UI.HtmlControls;
10
using
System.Data.SqlClient;
11
12
public
partial
class
Default5 : System.Web.UI.Page
13
{
14
//
清清月儿http:
//
blog.csdn.net/21aspnet
15
SqlConnection sqlcon;
16
string
strCon
=
"
Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa
"
;
17
protected
void
Page_Load(
object
sender, EventArgs e)
18
{
19
if
(
!
IsPostBack)
20
{
21
bind();
22
}
23
}
24
protected
void
CheckBox2_CheckedChanged(
object
sender, EventArgs e)
25
{
26
for
(
int
i
=
0
; i
<=
GridView1.Rows.Count
-
1
; i
++
)
27
{
28
CheckBox cbox
=
(CheckBox)GridView1.Rows[i].FindControl(
"
CheckBox1
"
);
29
if
(CheckBox2.Checked
==
true
)
30
{
31
cbox.Checked
=
true
;
32
}
33
else
34
{
35
cbox.Checked
=
false
;
36
}
37
}
38
}
39
protected
void
Button2_Click(
object
sender, EventArgs e)
40
{
41
sqlcon
=
new
SqlConnection(strCon);
42
SqlCommand sqlcom;
43
for
(
int
i
=
0
; i
<=
GridView1.Rows.Count
-
1
; i
++
)
44
{
45
CheckBox cbox
=
(CheckBox)GridView1.Rows[i].FindControl(
"
CheckBox1
"
);
46
if
(cbox.Checked
==
true
)
47
{
48
49
string
sqlstr
=
"
delete from 飞狐工作室 where 身份证号码='
"
+
GridView1.DataKeys[i].Value
+
"
'
"
;
50
sqlcom
=
new
SqlCommand(sqlstr, sqlcon);
51
sqlcon.Open();
52
sqlcom.ExecuteNonQuery();
53
sqlcon.Close();
54
}
55
}
56
bind();
57
}
58
protected
void
Button1_Click(
object
sender, EventArgs e)
59
{
60
CheckBox2.Checked
=
false
;
61
for
(
int
i
=
0
; i
<=
GridView1.Rows.Count
-
1
; i
++
)
62
{
63
CheckBox cbox
=
(CheckBox)GridView1.Rows[i].FindControl(
"
CheckBox1
"
);
64
cbox.Checked
=
false
;
65
}
66
}
67
public
void
bind()
68
{
69
string
sqlstr
=
"
select top 5 * from 飞狐工作室
"
;
70
sqlcon
=
new
SqlConnection(strCon);
71
SqlDataAdapter myda
=
new
SqlDataAdapter(sqlstr, sqlcon);
72
DataSet myds
=
new
DataSet();
73
sqlcon.Open();
74
myda.Fill(myds,
"
tb_Member
"
);
75
GridView1.DataSource
=
myds;
76
GridView1.DataKeyNames
=
new
string
[]
{
"
身份证号码
"
}
;
77
GridView1.DataBind();
78
sqlcon.Close();
79
}
80
}
前台主要代码:
1
<
asp:GridView
ID
="GridView1"
runat
="server"
AllowSorting
="True"
AutoGenerateColumns
="False"
2
CellPadding
="3"
Font-Size
="9pt"
BackColor
="White"
BorderColor
="#CCCCCC"
BorderStyle
="None"
BorderWidth
="1px"
>
3
<
FooterStyle
BackColor
="White"
ForeColor
="#000066"
/>
4
<
Columns
>
5
<
asp:TemplateField
>
6
<
ItemTemplate
>
7
<
asp:CheckBox
ID
="CheckBox1"
runat
="server"
/>
8
</
ItemTemplate
>
9
</
asp:TemplateField
>
10
<
asp:BoundField
DataField
="身份证号码"
HeaderText
="用户ID"
SortExpression
="身份证号码"
/>
11
<
asp:BoundField
DataField
="姓名"
HeaderText
="用户姓名"
SortExpression
="姓名"
/>
12
13
<
asp:BoundField
DataField
="家庭住址"
HeaderText
="家庭住址"
SortExpression
="家庭住址"
/>
14
15
</
Columns
>
16
<
RowStyle
ForeColor
="#000066"
/>
17
<
SelectedRowStyle
BackColor
="#669999"
Font-Bold
="True"
ForeColor
="White"
/>
18
<
PagerStyle
BackColor
="White"
ForeColor
="#000066"
HorizontalAlign
="Left"
/>
19
<
HeaderStyle
BackColor
="#006699"
Font-Bold
="True"
ForeColor
="White"
/>
20
</
asp:GridView
>
21
<
asp:CheckBox
ID
="CheckBox2"
runat
="server"
AutoPostBack
="True"
Font-Size
="9pt"
OnCheckedChanged
="CheckBox2_CheckedChanged"
22
Text
="全选"
/>
23
<
asp:Button
ID
="Button1"
runat
="server"
Font-Size
="9pt"
Text
="取消"
OnClick
="Button1_Click"
/>
24
<
asp:Button
ID
="Button2"
runat
="server"
Font-Size
="9pt"
Text
="删除"
OnClick
="Button2_Click"
/>
25
26
共
2
页 第
1
页
上一页
1
2
下一页
相关的教程:
GridView
技巧
GridView 72般绝技
收藏此教程
Currently.-0.05/5
-5
-4
-3
-2
-1
0
1
2
3
4
5
当前平均分:
0.0
(
4
次打分)
-5
-4
-3
-2
-1
0
1
2
3
4
5
推荐阅读
GridView 72般绝技(一)
GridView 72般绝技(一)
GridView 72般绝技(一)
c#开发-基础知识及有用技巧(一)
GridView 72般绝技(三)
在GridView显示时间列时,设置时间的格式
asp.net 局域网存放文件
扩展GridView控件(11) - 合并指定列的相邻且内容相同的单元格
GridView 72般绝技(五)
GridView 72般绝技(五)
添加评论
评论主题
您的大名
您的评论
验证码
评论列表
ASP.NET论坛
|
网站帮助
|
加入收藏
知识库搜索:
用户信息
欢迎您,游客。
登录
|
注册
为什么要注册?
马上加入GotAspx,建立自己的知识库,与大家分享您的知识库,还可获得丰厚积分奖励!
本类热门
从零开始学ASP.NET(基础篇)
ASP.NET 2.0轻松实现数据库应用开发
ASP.NET 程序中常用的三十三种代码
GridView 72般绝技(一)
对初学者的建议:ASP.NET技术的学习顺序
单点登录在ASP.NET上的简单实现
ASP.NET数据库编程快速入门之技术慨述
ASP.NET 2.0中构造个性化网页
本类最新
介绍SubSonic【转】
ASP.NET访问XML的例子
WEB开发者版本级别
基于.NET2.0的System.Net.Mail发送邮件Demo
Asp.Net 文件操作基类(读取,删除,批量拷贝,批量删除,写入)
c#生成与 追加xml
一个复杂的Eval()绑定
将服务器上的一个.doc文档另存为到客户端