主要就是利用datalist的ItemDataBound事件给嵌套在里面的datagrid绑定数据。
前台
<asp:datalist id="DataList1" runat="server" Width="100%" RepeatDirection="Horizontal" RepeatColumns="2"
DataKeyField="TID">
<ItemStyle VerticalAlign="Top"></ItemStyle>
<ItemTemplate>
<!-----top!!!!!!!!!!!!!!!!!!!!!!------->
<TABLE width="100%">
<TR>
<TD class="cpx12labout1" bgColor="#e5f1f8" height="20"><STRONG><IMG height="10" src="images/br_002.gif" width="24"><%# DataBinder.Eval(Container.DataItem, "TypeName") %></STRONG></TD>
</TR> <!------嵌套DataGrid------->
<TR vAlign="top">
<TD vAlign="top">
<asp:datagrid id="GridList1" runat="server" Width="95%" PageSize="5" ShowHeader="False" AllowPaging="True"
AutoGenerateColumns="False" BorderStyle="None" BorderWidth="0px" BackColor="White" CellPadding="3"
GridLines="Vertical">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<table>
<tr>
<td><IMG alt="" src="images/MiniSquare.gif"></td>
<td>
<a tips='<%# DataBinder.Eval(Container.DataItem, "Ntitle") %>' onmousemove=show(this.tips,1) onmouseout=show(this.tips,0) class="cpx12363636" href='javascript:newin("newsDetail.aspx?id=<%# DataBinder.Eval(Container.DataItem, "NID") %>",670,500)' >
<asp:Label Width="100%" CssClass="hand" runat="server" Text='<%# CutString1(DataBinder.Eval(Container.DataItem,"ntitle").ToString(),22) %>
' ID="Label4">
</asp:Label></a></td>
<td><font color="DarkGray">(<%#DataBinder.Eval(Container.DataItem,"CreateTime","{0:d}")%>)</font></td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle Visible="False" NextPageText="下一页" PrevPageText="上一页" HorizontalAlign="Center"></PagerStyle>
</asp:datagrid></TD>
</TR> <!------嵌套DataGrid结束------->
<TR>
<TD align="right"><A href='<%# String.Format("index_1.aspx?TID={0}",DataBinder.Eval(Container.DataItem, "[\"Tid\"]")) %>'><IMG style="BORDER-RIGHT: #ffffff 1px solid; BORDER-TOP: #ffffff 1px solid; BORDER-LEFT: #ffffff 1px solid; BORDER-BOTTOM: #ffffff 1px solid"
alt="" src="images/more.gif"> </A>
</TD>
</TR>
</TABLE>
</ItemTemplate>
</asp:datalist>
后台
private void DataList1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
// Label PriceLabel = (Label)e.Item.FindControl("Label1");
// PriceLabel.Text = DataList1.DataKeys[(int)e.Item.ItemIndex].ToString();
![]()
//得到当前类别的详细信息
DataTable tblNews = new DataTable();
DataTable tblTypeInfo = new DataTable();
int intTID = Convert.ToInt32(DataList1.DataKeys[(int)e.Item.ItemIndex]);
tblTypeInfo = cNews.GetTypeInfo(intTID);
//strTitle = tblTypeInfo.Rows[0]["TypeName"].ToString();
if (tblTypeInfo.Rows[0]["ParentID"].ToString()=="0")
{
//当前为一级类别,取所有属于此级的二级内容
tblNews = cNews.GetNewList(intTID);
}
else
{
//二级
tblNews = BLL.News.cNews.GetNewsByType(intTID.ToString());
}
![]()
DataGrid grid = (DataGrid)e.Item.FindControl("GridList1");
grid .DataSource=tblNews;
grid .DataBind();
![]()
}
前台
<asp:datalist id="DataList1" runat="server" Width="100%" RepeatDirection="Horizontal" RepeatColumns="2"
DataKeyField="TID">
<ItemStyle VerticalAlign="Top"></ItemStyle>
<ItemTemplate>
<!-----top!!!!!!!!!!!!!!!!!!!!!!------->
<TABLE width="100%">
<TR>
<TD class="cpx12labout1" bgColor="#e5f1f8" height="20"><STRONG><IMG height="10" src="images/br_002.gif" width="24"><%# DataBinder.Eval(Container.DataItem, "TypeName") %></STRONG></TD>
</TR> <!------嵌套DataGrid------->
<TR vAlign="top">
<TD vAlign="top">
<asp:datagrid id="GridList1" runat="server" Width="95%" PageSize="5" ShowHeader="False" AllowPaging="True"
AutoGenerateColumns="False" BorderStyle="None" BorderWidth="0px" BackColor="White" CellPadding="3"
GridLines="Vertical">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<table>
<tr>
<td><IMG alt="" src="images/MiniSquare.gif"></td>
<td>
<a tips='<%# DataBinder.Eval(Container.DataItem, "Ntitle") %>' onmousemove=show(this.tips,1) onmouseout=show(this.tips,0) class="cpx12363636" href='javascript:newin("newsDetail.aspx?id=<%# DataBinder.Eval(Container.DataItem, "NID") %>",670,500)' >
<asp:Label Width="100%" CssClass="hand" runat="server" Text='<%# CutString1(DataBinder.Eval(Container.DataItem,"ntitle").ToString(),22) %>
' ID="Label4">
</asp:Label></a></td>
<td><font color="DarkGray">(<%#DataBinder.Eval(Container.DataItem,"CreateTime","{0:d}")%>)</font></td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle Visible="False" NextPageText="下一页" PrevPageText="上一页" HorizontalAlign="Center"></PagerStyle>
</asp:datagrid></TD>
</TR> <!------嵌套DataGrid结束------->
<TR>
<TD align="right"><A href='<%# String.Format("index_1.aspx?TID={0}",DataBinder.Eval(Container.DataItem, "[\"Tid\"]")) %>'><IMG style="BORDER-RIGHT: #ffffff 1px solid; BORDER-TOP: #ffffff 1px solid; BORDER-LEFT: #ffffff 1px solid; BORDER-BOTTOM: #ffffff 1px solid"
alt="" src="images/more.gif"> </A>
</TD>
</TR>
</TABLE>
</ItemTemplate>
</asp:datalist>后台
private void DataList1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
// Label PriceLabel = (Label)e.Item.FindControl("Label1");
// PriceLabel.Text = DataList1.DataKeys[(int)e.Item.ItemIndex].ToString();
//得到当前类别的详细信息
DataTable tblNews = new DataTable();
DataTable tblTypeInfo = new DataTable();
int intTID = Convert.ToInt32(DataList1.DataKeys[(int)e.Item.ItemIndex]);
tblTypeInfo = cNews.GetTypeInfo(intTID);
//strTitle = tblTypeInfo.Rows[0]["TypeName"].ToString();
if (tblTypeInfo.Rows[0]["ParentID"].ToString()=="0")
{
//当前为一级类别,取所有属于此级的二级内容
tblNews = cNews.GetNewList(intTID);
}
else
{
//二级
tblNews = BLL.News.cNews.GetNewsByType(intTID.ToString());
}
DataGrid grid = (DataGrid)e.Item.FindControl("GridList1");
grid .DataSource=tblNews;
grid .DataBind();
}

浙公网安备 33010602011771号