- 爱易网页
-
ASP.NET教程
- asp.net中用Repeater控件这样显示数据怎么实现
日期:2014-05-18 浏览次数:20538 次
asp.net中用Repeater控件这样显示数据如何实现
新闻中心这个大栏目里有两个个栏目: 管理资讯和教学动态
如何实现这样的效果,问下思路?
如下图下效果:
http://www.smallfamily.cn/temp/pic.jpg
------解决方案--------------------
我明白了,Repeater嵌套可以解决。给个参考代码,呵呵
方法一:
<asp:DataList ID= "DataList1 " runat= "server " OnItemDataBound= "ItemDataBound ">
<ItemTemplate>
<asp:HiddenField ID= "hf " runat= "server " Value= ' <%# Eval( "type ") %> ' />
<div> <%# Eval( "name ") %> </div>
<div>
<asp:DataList ID= "dl1 " runat= "server ">
<ItemTemplate>
<%# Eval( "Name ") %>
</ItemTemplate>
</asp:DataList>
</div>
</ItemTemplate>
</asp:DataList>
public DataTable GetList(int typeId)
{
string connStr = "Provider=Microsoft.Jet.OleDb.4.0; Data Source= " + Server.MapPath( "data.mdb ");
string sql = "select * from [food] where [type]= " + typeId;
System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter(sql, connStr);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
protected void ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemIndex > -1)
{
DataList dl = (DataList)e.Item.FindControl( "dl1 ");
if (dl != null)
{
int typeId = int.Parse(((HiddenField)e.Item.FindControl( "hf ")).Value);
dl.DataSource = GetList(typeId);
dl.DataBind();
}
}
}
方法二:
<asp:DataList ID= "DataList2 " runat= "server " OnItemDataBound= "ItemDataBound ">
<ItemTemplate>
<asp:HiddenField ID= "hf " runat= "server " Value= ' <%# Eval( "type ") %> ' />
<div> <%# Eval( "name ") %> </div>
<div>
<asp:DataList ID= "dl1 " runat= "server " DataSource= ' <%# GetList(int.Parse(Eval( "type ").ToString())) %> '>
<ItemTemplate>
<%# Eval( "Name ") %>
</ItemTemplate>
</asp:DataList>
</div>
</ItemTemplate>
</asp:DataList>
------解决方案--------------------
前台
<asp:Repeater ID= "Repeater1 " runat= "server ">
<ItemTemplate>
<asp:Repeater ID= "Repeater1 " runat= "server " DataSource= ' <%# MyChild(Eval( "ID ").ToString()) %> '>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
ID为外面的repeater帮定的数据中的id
后台
protected datatable MyChild(string id)
{
//根据传进来的id来得到你想要的数据
}