会员
周边
新闻
博问
闪存
赞助商
Chat2DB
所有博客
当前博客
我的博客
我的园子
账号设置
会员中心
简洁模式
...
退出登录
注册
登录
着迷Linux
菲一打
在DataGrid中操作CheckBox控件
<
%@ Page Language
=
"
VB
"
AutoEventWireup
=
"
True
"
Debug
=
"
true
"
%
>
<
%@ Import
Namespace =
Namespace
=
"
System.Data
"
%
>
<
%@ Import
Namespace =
Namespace
=
"
System.Data.OleDb
"
%
>
<
script runat
=
"
server
"
>
dim
MyConnection
as
OleDbConnection
Sub Page_Load()
Sub
Page_Load(sender
As
Object
, e
As
EventArgs)
MyConnection
=
New
OleDbConnection(
"
Provider=Microsoft.Jet.OLEDB.4.0; Data Source =
"
+
Server.MapPath(
"
.
"
)
+
"
/db/data.mdb
"
)
If
Not
IsPostBack
Then
BindGrid()
End
If
End Sub
Sub BindGrid()
Sub
BindGrid()
Dim
MyCommand
As
OleDbDataAdapter
=
new
OleDbDataAdapter(
"
select * from [user]
"
, MyConnection)
Dim
DS
As
DataSet
=
new
DataSet()
MyCommand.Fill(DS,
"
user
"
)
MyDataGrid.DataSource
=
DS.Tables(
"
user
"
).DefaultView
MyDataGrid.DataBind()
End Sub
Public
Sub Show_Selected()
Sub
Show_Selected(
ByVal
sender
As
Object
,
ByVal
e
As
EventArgs)
For
Each
di
As
DataGridItem
In
MyDataGrid.Items
Dim
cb
As
HtmlInputCheckBox
=
CType
(di.FindControl(
"
contract
"
), HtmlInputCheckBox)
If
Not
(cb
Is
Nothing
)
AndAlso
cb.Checked
Then
Dim
lb
As
Label
=
CType
(di.FindControl(
"
lblname
"
), Label)
Response.
Write
(di.Cells(
1
).Text
+
"
--
"
+
lb.Text
+
"
<br>
"
)
End
If
Next
End Sub
</
script
>
<
body style
=
"
font: 10pt verdana
"
>
<
form runat
=
"
server
"
id
=
"
form1
"
name
=
"
form1
"
>
<
ASP:DataGrid id
=
"
MyDataGrid
"
runat
=
"
server"
Width
=
"
200"
BackColor
=
"
#ccccff"
BorderColor
=
"
black"
ShowFooter
=
"
false"
CellPadding
=
3
CellSpacing
=
"
0"
Font
-
Name
=
"
Verdana"
Font
-
Size
=
"
8pt"
HeaderStyle
-
BackColor
=
"
#aaaadd"
AutoGenerateColumns
=
"
false"
>
<
Columns
>
<
asp:TemplateColumn HeaderText
=
"
contract
"
>
<
HeaderTemplate
>
<
input
type
=
checkbox id
=
"
checkAll
"
onclick
=
"
CheckAll(this);
"
runat
=
"
server
"
>
Contracts
</
HeaderTemplate
>
<
ItemTemplate
>
<
input
type
=
checkbox runat
=
"
server
"
id
=
"
contract
"
onclick
=
"
UnCheck();
"
/>
</
ItemTemplate
>
</
asp:TemplateColumn
>
<
asp:BoundColumn HeaderText
=
"
id
"
DataField
=
"
ID
"
/>
<
asp:TemplateColumn HeaderText
=
"
Name
"
>
<
ItemTemplate
>
<
asp:Label id
=
"
lblName"
Text
=
'
<%# DataBinder.Eval(Container.DataItem, "name") %>'
runat
=
"
server
"
/>
</
ItemTemplate
>
</
asp:TemplateColumn
>
</
Columns
>
</
ASP:DataGrid
>
<
asp:button Text
=
"
List Selected
"
runat
=
"
server
"
OnClick
=
"
Show_Selected
"
/>
<
script
>
var frm
=
document.form1 ;
Function CheckAll()
function
CheckAll( checkAllBox )
{
var actVar
=
checkAllBox.checked ;
for
(i
=
0
;i
<
frm.length;i
++
)
{
e
=
frm.elements[i];
if
( e.type
==
'
checkbox' && e.name.indexOf("contract") != -1 )
e.checked
=
actVar ;
}
}
Function UnCheck()
function
UnCheck()
{
for
(i
=
0
;i
<
frm.length;i
++
)
{
e
=
frm.elements[i];
if
( e.type
==
'
checkbox' && e.name.indexOf("checkAll") != -1 )
{
e.checked
=
false
;
break;
}
}
}
</
script
>
</
form
>
</
body
>
发表于
2005-03-05 17:12
菲一打
阅读(
1292
) 评论(
0
)
收藏
举报
刷新页面
返回顶部
公告
导航
博客园
首页
新随笔
新文章
联系
订阅
管理