今天做了一个GridView内嵌套DataList的东东。GirdView来显示电影类别,DataList用来显示一个类别中的9部电影(3行3列)。
先来看一下数据源的大概结构:
要用到的是Movie和MovieType表。
创建两个BLL方法,分别用于选出Type表中全部的类别以及某一类别的全部影片:
using System;
using System.Collections.Generic;
using System.Text;
using QmxMovieDAL;
using QmxMovieDAL.QmxMovieTableAdapters;
namespace QmxMovieBLL
{
[System.ComponentModel.DataObject]
public class MovieTypeBLL
{
private MovieTypeTableAdapter adapter = null;
protected MovieTypeTableAdapter Adapter
{
get
{
if (adapter == null) adapter = new MovieTypeTableAdapter();
return adapter;
}
}
/// <summary>
/// 获取全部影片类别
/// </summary>
/// <returns></returns>
[System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select,true)]
public QmxMovie.MovieTypeDataTable GetMovieTypes()
{
return Adapter.GetMovieTypes();
}
//其它方法
}
}
using System.Collections.Generic;
using System.Text;
using QmxMovieDAL;
using QmxMovieDAL.QmxMovieTableAdapters;
namespace QmxMovieBLL
{
[System.ComponentModel.DataObject]
public class MovieTypeBLL
{
private MovieTypeTableAdapter adapter = null;
protected MovieTypeTableAdapter Adapter
{
get
{
if (adapter == null) adapter = new MovieTypeTableAdapter();
return adapter;
}
}
/// <summary>
/// 获取全部影片类别
/// </summary>
/// <returns></returns>
[System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select,true)]
public QmxMovie.MovieTypeDataTable GetMovieTypes()
{
return Adapter.GetMovieTypes();
}
//其它方法
}
}
using System;
using System.Collections.Generic;
using System.Text;
using QmxMovieDAL;
using QmxMovieDAL.QmxMovieTableAdapters;
namespace QmxMovieBLL
{
[System.ComponentModel.DataObject]
public class MovieBLL
{
private MovieTableAdapter adapter = null;
protected MovieTableAdapter Adapter
{
get
{
if (adapter == null) adapter = new MovieTableAdapter();
return adapter;
}
}
/// <summary>
/// 返回前9条分类的可见的电影信息
/// </summary>
/// <param name="typeID">类别编号</param>
/// <returns></returns>
[System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select,false)]
public QmxMovie.MovieDataTable GetTop9VisiableByTypeID(int typeID)
{
return Adapter.GetTop9MoviesByTypeID(typeID);
}
//其他方法
}
}
然后,在显示页面上添加一个GridView、一个ObjectDataSource:using System.Collections.Generic;
using System.Text;
using QmxMovieDAL;
using QmxMovieDAL.QmxMovieTableAdapters;
namespace QmxMovieBLL
{
[System.ComponentModel.DataObject]
public class MovieBLL
{
private MovieTableAdapter adapter = null;
protected MovieTableAdapter Adapter
{
get
{
if (adapter == null) adapter = new MovieTableAdapter();
return adapter;
}
}
/// <summary>
/// 返回前9条分类的可见的电影信息
/// </summary>
/// <param name="typeID">类别编号</param>
/// <returns></returns>
[System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select,false)]
public QmxMovie.MovieDataTable GetTop9VisiableByTypeID(int typeID)
{
return Adapter.GetTop9MoviesByTypeID(typeID);
}
//其他方法
}
}
1 <asp:GridView ID="gvTypeList" runat="server" AutoGenerateColumns="False" DataKeyNames="TypeID"
2 DataSourceID="odsMovieTypeList" OnRowDataBound="gvTypeList_RowDataBound" ShowHeader="False" Width="95%">
3 <Columns>
4 <asp:BoundField DataField="TypeName" HeaderText="TypeName" SortExpression="TypeName" >
5 <ItemStyle HorizontalAlign="Center" VerticalAlign="Top" Width="80px" />
6 <HeaderStyle VerticalAlign="Top" Width="80px" />
7 </asp:BoundField>
8 <asp:TemplateField>
9 <ItemTemplate>
10 <!--准备添加DataList-->
11 </ItemTemplate>
12 </asp:TemplateField>
13 </Columns>
14 </asp:GridView>
15 <asp:ObjectDataSource ID="odsMovieTypeList" runat="server" DeleteMethod="DeleteMovieType"
16 InsertMethod="InsertMovieType" OldValuesParameterFormatString="{0}"
17 SelectMethod="GetMovieTypesVisiable" TypeName="QmxMovieBLL.MovieTypeBLL" UpdateMethod="UpdateMovieType">
18 <DeleteParameters>
19 <asp:Parameter Name="typeID" Type="Int32" />
20 </DeleteParameters>
21 </asp:ObjectDataSource>
2 DataSourceID="odsMovieTypeList" OnRowDataBound="gvTypeList_RowDataBound" ShowHeader="False" Width="95%">
3 <Columns>
4 <asp:BoundField DataField="TypeName" HeaderText="TypeName" SortExpression="TypeName" >
5 <ItemStyle HorizontalAlign="Center" VerticalAlign="Top" Width="80px" />
6 <HeaderStyle VerticalAlign="Top" Width="80px" />
7 </asp:BoundField>
8 <asp:TemplateField>
9 <ItemTemplate>
10 <!--准备添加DataList-->
11 </ItemTemplate>
12 </asp:TemplateField>
13 </Columns>
14 </asp:GridView>
15 <asp:ObjectDataSource ID="odsMovieTypeList" runat="server" DeleteMethod="DeleteMovieType"
16 InsertMethod="InsertMovieType" OldValuesParameterFormatString="{0}"
17 SelectMethod="GetMovieTypesVisiable" TypeName="QmxMovieBLL.MovieTypeBLL" UpdateMethod="UpdateMovieType">
18 <DeleteParameters>
19 <asp:Parameter Name="typeID" Type="Int32" />
20 </DeleteParameters>
21 </asp:ObjectDataSource>
第17行,SelectMethod指向MovieTypeBLL中的GetMovieTypesVisiable方法。TypeName设定使用的BLL。
注意一下第10行,添加了一个模板列,这个位置就是空着准备放DataList的。
将下列代码添加到上述第10行位置:
1<asp:DataList ID="dlMovies" runat="server" DataKeyField="MovieID" DataSourceID="odsTop9TypedMovie" RepeatColumns="3" RepeatDirection="Horizontal" Width="100%">
2 <ItemTemplate>
3 <asp:HyperLink ID="hlMovieName" runat="server" Text='<%# Net.Qmx.Evil.StrUtility.TruncStr(Eval("MovieName").ToString(), 15) %>' NavigateUrl='<%# Eval("MovieID", "MovieDetails.aspx?MovieID={0}") %>' />
4 </ItemTemplate>
5</asp:DataList>
6<asp:ObjectDataSource ID="odsTop9TypedMovie" runat="server"
7 SelectMethod="GetTop9VisiableByTypeID" TypeName="QmxMovieBLL.MovieBLL">
8 <SelectParameters>
9 <asp:Parameter Name="typeID" Type="Int32" />
10 </SelectParameters>
11</asp:ObjectDataSource>
注意第9行,此ObjectDataSource需要一个参数typeID,此参数恰好为DataList所在行的类别的类别编号(TypeID)。而GridView的OnRowDataBound事件触发于一行数据已经绑定到GridView上以后,此时,已经可以访问到此GridViewRow上的数据,故,在GridView的OnRowDataBound事件处理中添加以下代码:2 <ItemTemplate>
3 <asp:HyperLink ID="hlMovieName" runat="server" Text='<%# Net.Qmx.Evil.StrUtility.TruncStr(Eval("MovieName").ToString(), 15) %>' NavigateUrl='<%# Eval("MovieID", "MovieDetails.aspx?MovieID={0}") %>' />
4 </ItemTemplate>
5</asp:DataList>
6<asp:ObjectDataSource ID="odsTop9TypedMovie" runat="server"
7 SelectMethod="GetTop9VisiableByTypeID" TypeName="QmxMovieBLL.MovieBLL">
8 <SelectParameters>
9 <asp:Parameter Name="typeID" Type="Int32" />
10 </SelectParameters>
11</asp:ObjectDataSource>
1protected void gvTypeList_RowDataBound(object sender, GridViewRowEventArgs e)
2{
3 if (e.Row.RowType == DataControlRowType.DataRow)
4 {
5 ObjectDataSource odsMovie = (ObjectDataSource)e.Row.Cells[1].FindControl("odsTop9TypedMovie");
6 DataList dlMovie = (DataList)e.Row.Cells[1].FindControl("dlMovies");
7 QmxMovie.MovieTypeRow movieType = (QmxMovie.MovieTypeRow)((DataRowView)e.Row.DataItem).Row;
8 odsMovie.SelectParameters["typeID"].DefaultValue = movieType.TypeID.ToString();
9 dlMovie.DataBind();
10
11 }
12}
通过,第7行,我们把e.Row.DataItem转成DataRowView对象,取其Row并转成强类型的MovieTypeRow,赋给movieType对象,然后,下一行就用它的TypeID属性,获得其类别编号并赋给DataList所使用的ObjectDataSource的Select参数。最后对DataList进行数据绑定。2{
3 if (e.Row.RowType == DataControlRowType.DataRow)
4 {
5 ObjectDataSource odsMovie = (ObjectDataSource)e.Row.Cells[1].FindControl("odsTop9TypedMovie");
6 DataList dlMovie = (DataList)e.Row.Cells[1].FindControl("dlMovies");
7 QmxMovie.MovieTypeRow movieType = (QmxMovie.MovieTypeRow)((DataRowView)e.Row.DataItem).Row;
8 odsMovie.SelectParameters["typeID"].DefaultValue = movieType.TypeID.ToString();
9 dlMovie.DataBind();
10
11 }
12}
一个内嵌了DataList的GridView就完成了。
感谢lg的提醒,最后附加一张效果图(还没有美化过:-)