№.零零伍
博客园
首页
社区
新随笔
联系
订阅
管理
随笔-17 评论-99 文章-16 trackbacks-101
GridView模版列嵌套GirdView显示主从表数据
当需要在一个列表中显示主从表(例如部门-人员的信息),在asp.net1.1中我们可能会使用DataGrid模版列嵌套DataGrid的方法实现,然而,处理模版列里的DataGrid的翻页、排序、编辑等功能时都比较麻烦。在asp.net2.0中,配合DataSource控件的使用让这个问题变得非常简单!
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
GridView_GirdView.aspx.cs
"
Inherits
=
"
GridSamples_GridView_GirdView
"
%>
<!
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
>
<
asp:GridView
ID
="GridView1"
runat
="server"
AutoGenerateColumns
="False"
DataKeyNames
="deptid"
DataSourceID
="AccessDataSource1"
AllowPaging
="True"
AllowSorting
="True"
PageSize
="2"
OnRowDataBound
="GridView1_RowDataBound"
>
<
Columns
>
<
asp:BoundField
DataField
="deptid"
HeaderText
="部门编号"
InsertVisible
="False"
ReadOnly
="True"
SortExpression
="deptid"
/>
<
asp:BoundField
DataField
="deptname"
HeaderText
="部门名称"
SortExpression
="deptname"
/>
<
asp:BoundField
DataField
="deptremark"
HeaderText
="备注"
SortExpression
="deptremark"
/>
<
asp:TemplateField
HeaderText
="人员信息"
>
<
ItemTemplate
>
<
asp:GridView
ID
="GridView2"
runat
="server"
AutoGenerateColumns
="False"
DataKeyNames
="id"
DataSourceID
="AccessDataSource2"
AllowPaging
="True"
AllowSorting
="True"
PageSize
="5"
>
<
Columns
>
<
asp:BoundField
DataField
="id"
HeaderText
="人员编号"
InsertVisible
="False"
ReadOnly
="True"
SortExpression
="id"
/>
<
asp:BoundField
DataField
="name"
HeaderText
="姓名"
SortExpression
="name"
/>
<
asp:BoundField
DataField
="sex"
HeaderText
="性别"
SortExpression
="sex"
/>
</
Columns
>
<
PagerSettings
FirstPageText
="首页"
LastPageText
="末页"
Mode
="NextPreviousFirstLast"
NextPageText
="下一页"
PreviousPageText
="上一页"
/>
</
asp:GridView
>
<
asp:AccessDataSource
ID
="AccessDataSource2"
runat
="server"
DataFile
="~/App_Data/test.mdb"
SelectCommand
="SELECT [id], [name], [sex], [deptid] FROM [employees] WHERE ([deptid] = ?)"
>
<
SelectParameters
>
<
asp:Parameter
Name
="deptid"
Type
="Int32"
/>
</
SelectParameters
>
</
asp:AccessDataSource
><
br
>
</
ItemTemplate
>
</
asp:TemplateField
>
</
Columns
>
<
PagerSettings
FirstPageText
="首页"
LastPageText
="末页"
NextPageText
="下一页"
PreviousPageText
="上一页"
/>
</
asp:GridView
>
<
asp:AccessDataSource
ID
="AccessDataSource1"
runat
="server"
DataFile
="~/App_Data/test.mdb"
SelectCommand
="SELECT [deptid], [deptname], [deptremark], [createdate] FROM [departments]"
>
</
asp:AccessDataSource
>
</
div
>
</
form
>
</
body
>
</
html
>
1
using
System;
2
using
System.Data;
3
using
System.Configuration;
4
using
System.Collections;
5
using
System.Web;
6
using
System.Web.Security;
7
using
System.Web.UI;
8
using
System.Web.UI.WebControls;
9
using
System.Web.UI.WebControls.WebParts;
10
using
System.Web.UI.HtmlControls;
11
12
public
partial
class
GridSamples_GridView_GirdView : System.Web.UI.Page
13
{
14
protected
void
GridView1_RowDataBound(
object
sender, GridViewRowEventArgs e)
15
{
16
if
(e.Row.RowIndex
>
-
1
)
17
{
18
AccessDataSource accessDS
=
e.Row.FindControl(
"
AccessDataSource2
"
)
as
AccessDataSource;
19
accessDS.SelectParameters[
"
deptid
"
].DefaultValue
=
e.Row.Cells[
0
].Text;
20
}
21
}
22
}
只需要上面几行简单的代码便可以实现。
0
0
0
(请您对文章做出评价)
«
上一篇:
GridView中添加一个Radio列
»
下一篇:
母版页可以动态切换吗?
posted on 2006-07-04 22:28
№.零零伍
阅读(10314)
评论(3)
编辑
收藏
所属分类:
CSDN常见问题集
注册用户登录后才能发表评论,请
登录
或
注册
。
博客园首页
IT新闻
闪存
知识库
招聘
找优秀程序员,就在博客园
IT新闻
:
·
2008-2010中文SNS的观察和实践
·
GDC2010:暴雪的游戏设计的核心原则
·
Windows Azure:迫近的成功还是最终的利基?
·
百度股价大涨超越Google
·
Web标准化交流会3月27日四城市同时举行
每天10分钟,轻松学英语
专题:
Android
iPad
jQuery
Windows 7
推荐职位
:
网站导航:
博客园首页
IT新闻
个人主页
闪存
程序员招聘
社区
博问
网摘
China-pub 计算机图书网上专卖店!6.5万品种2-8折!
China-Pub 计算机绝版图书按需印刷服务
相关搜索:
CSDN常见问题集
在知识库中查看:
GridView模版列嵌套GirdView显示主从表数据
我的主页
个人资料
我的闪存
发短消息
搜索
常用链接
我的随笔
我的空间
我的短信
我的评论
更多链接
我的参与
我的新闻
最新评论
我的标签
随笔分类
(17)
CSDN常见问题集(7)
感悟零零伍(1)
技术文章-翻译(8)
转行卖茅厕了~(1)
随笔档案
(17)
2007年8月 (1)
2006年8月 (5)
2006年7月 (8)
2006年6月 (3)
文章分类
(12)
Project Management(12)
相册
零零伍的幸福人生
积分与排名
积分 - 141267
排名 - 484
阅读排行榜
1. Scott Mitchell 的ASP.NET 2.0数据教程之四:: 使用ObjectDataSource展现数据(19260)
2. 【翻译】Scott Mitchell的ASP.NET2.0数据教程中文版索引 (16414)
3. Scott Mitchell 的ASP.NET 2.0数据教程之十六::概述插入、更新和删除数据(15611)
4. Scott Mitchell 的ASP.NET 2.0数据教程之二十一:: 实现开放式并发(12548)
5. GridView中添加一个CheckBox列(11648)
6. Scott Mitchell 的ASP.NET 2.0数据教程之十八:: 在ASP.NET页面中处理BLL/DAL层的异常(11592)
7. Scott Mitchell 的ASP.NET 2.0数据教程之六:: 编程设置ObjectDataSource的参数值(10720)
8. GridView模版列嵌套GirdView显示主从表数据(10314)
9. Scott Mitchell 的ASP.NET 2.0数据教程之五:: 声明参数(8951)
10. Scott Mitchell 的ASP.NET 2.0数据教程之十七:: 研究插入、更新和删除的关联事件(8602)
11. 收录了一篇关于项目管理九大知识领域的比较详细的教程~(1933)
12. 母版页可以多层嵌套使用~(1856)
13. 关于 DataControlRowType枚举 和 DataControlRowState 枚举(1173)
14. GridView中添加一个Radio列 (1133)
15. 母版页可以动态切换吗?(1022)
16. asp.net 2.0控件一些有可能是bug的小问题:(799)
17. http://www.heshunjc.cn/(323)