Repeater中嵌套Repeater的问题

x03570227 2006-06-25 11:14:53
我想做个分类显示数据的页面
如:
类别1
abcdeft
abcdeft
abcdeft
abcdeft
...
类别2
zycnmgd
zycnmgd
zycnmgd
...
...
类别存在一张表里,分类的东西存在另一张表里,两张表之前通过sortid(分类ID)联系在一起
页面用的排版方式是DIV+CSS,页面上用repeater控件显示数据,单单一类数据的显示可以做的,但现在想做多个类别,而且有几个类别,类别下面的东西也不固定,全都按照数据库里来定,这样的话不可能在页面上放N个控件,用到几个绑定几个,我的想法是放两个repeater,一个嵌套在里面,网上也找了些文章看了,看到一个方法,具体的在
http://www.alixixi.com/weBuild/subject4/200512131411880.html
我稍微改到了一下代码,把里面的数据绑定数据的以及数据的读取换成了自己的方式,没有成功,系统老是报rptProduct(嵌套在里面的repeater控件)没有datasource这个属性,我看了这个repeater,确实没有这个属性
我想问一下
是不是因为repeater控件嵌套在里面才没有那个属性的
如果是因为这个原因那不是不能用这个方式也显示数据?
对于这种按类别显示数据的页面,大家是怎么做的,能不能给些思路,不管是用datagrid还是用datalist
如果有必要我会把我的代码给贴出来
...全文
1576 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
x03570227 2006-06-28
  • 打赏
  • 举报
回复
问题解决了,在另一个帖子里,谢谢csdn的朋友
ldw701 2006-06-27
  • 打赏
  • 举报
回复
<table cellPadding="0" width="100%" align="center" border="0">
<tr>
<td vAlign="top" Width="100%"><asp:datalist id="ParentDataList" DataKeyField="ID" RepeatDirection="Vertical" RepeatColumns="1"
Runat="server" Width="100%">
<ItemStyle CssClass="text_k_12_3"></ItemStyle>
<ItemTemplate>
<table cellSpacing="0" cellPadding="0" width="100%" align="center" border="0">
<tr>
<td width="16"><IMG height="9" src="http://www.wz360.cn/Images/Module/wzsy_31.gif" width="9"></td>
<td class="text_k_12_3" align="left"><%# GetString(1,Convert.ToInt32(DataBinder.Eval(Container.DataItem,"Id")))%></td>
</tr>
</table>
<table width="85%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td align="center" width="100%">
<asp:DataList ID="ClassDataList" Runat="server" RepeatColumns="2" RepeatDirection="Horizontal"
HorizontalAlign="Center" RepeatLayout="Table" Width="100%" CssClass="text_k_12_3">
<ItemTemplate>
<span class="text_b_12_5">[<%# GetString(0,Convert.ToInt32(DataBinder.Eval(Container.DataItem,"Id")))%>]</span>
</ItemTemplate>
</asp:DataList>
</td>
</tr>
</table>
</ItemTemplate>
</asp:datalist></td>
</tr>
</table>

namespace URL.UserControl
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Url.Business;
using Url.Common;

/// <summary>
/// ClassIndex 的摘要说明。
/// </summary>
public class ClassIndex : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.DataList ParentDataList;


private int _AreaId;
public int AreaId
{
get{return this._AreaId;}
set{this._AreaId = value;}
}
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (!Page.IsPostBack)
{
DataBindParentClass();
}
}

private void DataBindParentClass()
{
Url.Business.Class cls = new Url.Business.Class();
DataTable objDt = cls.GetParentClass(this._AreaId,0,0,"");
ParentDataList.DataSource = objDt.DefaultView;
ParentDataList.DataBind();
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.ParentDataList.ItemDataBound += new System.Web.UI.WebControls.DataListItemEventHandler(this.ParentDataList_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void ParentDataList_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataList SubDataList = (DataList)e.Item.FindControl("ClassDataList");
int ParentId = Convert.ToInt32(ParentDataList.DataKeys[e.Item.ItemIndex]);
Url.Business.Class cls = new Url.Business.Class();
DataTable dt = cls.GetSmallClass(ParentId,0,"");

SubDataList.DataSource = dt.DefaultView;
SubDataList.DataBind();

}
}

public string GetString(int IsParentClass,int ClassId)
{
string AreaEName;
string ClassEName;
string ReturnValue=string.Empty;
DataTable objDt = new Url.Business.Area().GetAreaById(AreaId);
if(objDt.Rows.Count >0)
AreaEName = objDt.Rows[0]["AreaDir"].ToString();
else
AreaEName = "all";//全国站
//AreaEName = "test";
Url.Business.Class Cls = new Url.Business.Class();
Cls.Id = Convert.ToInt32(ClassId);
DataTable objClassName = Cls.GetClassInfo("ClassName");
ClassEName = objClassName.Rows[0]["EName"].ToString();
if(AreaEName.ToLower()=="all")
{
//if (IsParentClass==1)
ReturnValue = "<a href=\"Class/"+ClassEName+".html\" target=\"_blank\">"+objClassName.Rows[0]["ClassName"].ToString()+"</a>";
//else
//{
// Url.Business.Class Class = new Url.Business.Class();
// Class.Id = Convert.ToInt32(objClassName.Rows[0]["ParentId"]);
// DataTable objParentClass = Class.GetClassInfo("ParentClassInfo");
// ReturnValue = "<a href=\"Class/"+objParentClass.Rows[0]["EName"].ToString()+".html#"+ClassEName+"\">"+objClassName.Rows[0]["ClassName"].ToString()+"</a>";
//}
}
else
{
//if (IsParentClass==1)
ReturnValue = "<a href=\"Class/"+ClassEName+".html\" target=\"_blank\">"+objClassName.Rows[0]["ClassName"].ToString()+"</a>";
//else
//{
// Url.Business.Class Class = new Url.Business.Class();
// Class.Id = Convert.ToInt32(objClassName.Rows[0]["ParentId"]);
// DataTable objParentClass = Class.GetClassInfo("ParentClassInfo");
// ReturnValue = "<a href=\"Class/"+objParentClass.Rows[0]["EName"].ToString()+".html#"+ClassEName+"\">"+objClassName.Rows[0]["ClassName"].ToString()+"</a>";
//}
}
return ReturnValue;
}
}
}
nnh 2006-06-26
  • 打赏
  • 举报
回复
.aspx
--------------------
<%@ Page language="c#" Src="NestedRepeater.aspx.cs" AutoEventWireup="false" Inherits="NestedRepeater.NestedRepeater" %>
<%@ Import Namespace="System.Data" %>

<html>
<body>
<form runat=server>

<!-- start parent repeater -->
<asp:repeater id="parentRepeater" runat="server">
<itemtemplate>
<b><%# DataBinder.Eval(Container.DataItem,"au_id") %></b><br>

<!-- start child repeater -->
<asp:repeater id="childRepeater" datasource='<%# ((DataRowView)Container.DataItem)
.Row.GetChildRows("myrelation") %>' runat="server">

<itemtemplate>
<%# DataBinder.Eval(Container.DataItem, "[\"title_id\"]")%><br>
</itemtemplate>
</asp:repeater>
<!-- end child repeater -->

</itemtemplate>
</asp:repeater>
<!-- end parent repeater -->

</form>
</body>
</html>


.aspx.cs
-------------
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NestedRepeater
{
public class NestedRepeater : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Repeater parentRepeater;
public NestedRepeater()
{
Page.Init += new System.EventHandler(Page_Init);
}
public void Page_Load(object sender, EventArgs e)
{
//Create the connection and DataAdapter for the Authors table.
SqlConnection cnn = new SqlConnection("server=p085;Initial Catalog=pubs;uid=sa;pwd=");
SqlDataAdapter cmd1 = new SqlDataAdapter("select * from authors",cnn);

//Create and fill the DataSet.
DataSet ds = new DataSet();
cmd1.Fill(ds,"authors");

//Create a second DataAdapter for the Titles table.
SqlDataAdapter cmd2 = new SqlDataAdapter("select * from titleauthor",cnn);
cmd2.Fill(ds,"titles");

//Create the relation bewtween the Authors and Titles tables.
ds.Relations.Add("myrelation",
ds.Tables["authors"].Columns["au_id"],
ds.Tables["titles"].Columns["au_id"]);

//Bind the Authors table to the parent Repeater control, and call DataBind.
parentRepeater.DataSource = ds.Tables["authors"];
Page.DataBind();

//Close the connection.
cnn.Close();
}
private void Page_Init(object sender, EventArgs e)
{
InitializeComponent();
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
}
}

welland 2006-06-26
  • 打赏
  • 举报
回复
noyester(继续是菜鸟)
所言正是
welland 2006-06-26
  • 打赏
  • 举报
回复
使用冒泡方法
x03570227 2006-06-26
  • 打赏
  • 举报
回复
唉,是我菜还是这个方法没人用
xyunsh 2006-06-25
  • 打赏
  • 举报
回复
lz可以参照一下,这个是用DataList的
注意一下嵌套控件的数据源绑定
DataSource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("type_pics") %>'>
xyunsh 2006-06-25
  • 打赏
  • 举报
回复
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if( !this.IsPostBack )
{
dlClientBind();
}
}

private void dlClientBind()
{
DataSet objDs = new DataSet();

DataTable dtClientType = DBTools.ConvertDataReaderToDataTable( HistoryTypeDB.GetAll());
dtClientType.TableName = "ClientType";
objDs.Tables.Add( dtClientType );

DataTable dtPicture = DBTools.ConvertDataReaderToDataTable( HistoryDB.GetAllTop( TopNum ) );
dtPicture.TableName = "Picture";
objDs.Tables.Add( dtPicture );

DataColumn Parent = objDs.Tables["ClientType"].Columns["ID"];
DataColumn Child = objDs.Tables["Picture"].Columns["Type"];
objDs.Relations.Add( "type_pics", Parent, Child );

dlClient.DataSource = objDs.Tables["ClientType"].DefaultView;
dlClient.DataBind();
}
xyunsh 2006-06-25
  • 打赏
  • 举报
回复
<asp:DataList id="dlClient" runat="server" Width="100%" RepeatColumns="1">
<ItemTemplate>
<TABLE width="96%">
<tr height="10">
<td colspan="2"></td>
</tr>
<TR>
<TD><IMG height="9" src="images/icon_01.gif" width="10"> <a href='<%# "kehuMorelist.aspx?type=" + DataBinder.Eval(Container.DataItem, "ID") +"&name=" + System.Web.HttpContext.Current.Server.UrlEncode(DataBinder.Eval(Container.DataItem, "Descs").ToString())%>'><%# DataBinder.Eval(Container.DataItem, "Descs") %></a></TD>
<td align="right"><a href='<%# "kehuMorelist.aspx?type=" + DataBinder.Eval(Container.DataItem, "ID")+"&name=" + System.Web.HttpContext.Current.Server.UrlEncode(DataBinder.Eval(Container.DataItem, "Descs").ToString()) %>'><img src='images/more_02.gif' width='49' height='17' border='0'></a></td>
</TR>
<TR>
<TD background="images/index_22.jpg" height="1" colspan="2"></TD>
</TR>
<TR>
<TD colspan="2">
<asp:DataList id="dlPicture" runat="server" Width="100%" RepeatColumns="1" RepeatDirection="Horizontal" DataSource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("type_pics") %>'>
<ItemTemplate>
<table width="90%">
<tr>
<td><img src='<%# "../SubModule/Files/Pic/Prod/Product/History/" + DataBinder.Eval(Container.DataItem, "[\"FileName\"]") %>' width="500" onerror='this.src="../SubModule/Files/Pic/Prod/Product/History/error.jpg"'></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList></TD>
</TR>
<TR>
<TD height="10" colspan="2"></TD>
</TR>
</TABLE>
</ItemTemplate>
</asp:DataList>
x03570227 2006-06-25
  • 打赏
  • 举报
回复
我的做法跟noyester(继续是菜鸟)的差不多,只不过是C#的,数据是从数据库里读取的
现在运行不起来,系统老是提示
F:\wwwroot\vsnettest\RpinRp.aspx.cs(78): “vsnettest.Repeater”并不包含对“DataSource”的定义
78行的代码是:
rptProduct.DataSource = myds.Tables["loginfo"].DefaultView;
这个Repeater控件是嵌套在里面的控件.

我还是把代码贴出来吧,不然都不知道怎么回事,呵呵
前台界面的主要代码
<asp:repeater id="rptCategories" runat="server">
<HeaderTemplate>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
</HeaderTemplate>
<ItemTemplate>
<!--分类名称-->
<tr>
<th>
<%# DataBinder.Eval(Container.DataItem, "Sortname") %>
</th>
</tr>
<!--分类下的产品-->
<asp:Repeater id="rptProduct" runat="server">
<ItemTemplate>
<tr>
<td><a href='ProductInfo.aspx?Id=<%# DataBinder.Eval(Container.DataItem, "logID") %>'><%# DataBinder.Eval(Container.DataItem, "logname") %></a></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:repeater>

后台的主要代码:
private void rptCategories_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
SqlConnection myconn = new SqlConnection("server=(local);Initial Catalog=web%5cnet;UID=webnet;PWD=");
SqlDataAdapter mydt = new SqlDataAdapter("select fname, lname FROM employee",myconn);
DataSet myds = new DataSet();
SqlCommand mycmd = new SqlCommand();
mycmd.Connection=myconn;
mycmd.CommandText="select * from logSort ";
mydt.SelectCommand=mycmd;
mydt.Fill(myds,"logsort");
rptCategories.DataSource = myds.Tables["logsort"].DefaultView;
rptCategories.DataBind();

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater rptProduct = (Repeater) e.Item.FindControl("rptProduct");
//找到分类Repeater关联的数据项
DataRowView rowv = (DataRowView)e.Item.DataItem;
//提取分类ID
int CategorieId = Convert.ToInt32(rowv["sortID"]);
//根据分类ID查询该分类下的产品,并绑定产品Repeater
mycmd.CommandText="select * from loginfo where sortid = "+CategorieId.ToString();
mydt.SelectCommand=mycmd;
mydt.Fill(myds,"loginfo");

rptProduct.DataSource = myds.Tables["loginfo"].DefaultView; //就是这个地方提示rptProduct不包含对datasource的定义.
rptProduct.DataBind();
}

}
x03570227 2006-06-25
  • 打赏
  • 举报
回复
谢谢,我再试试,今天去外面朋友那了,他那没装vs.net所以也没好好试,现在我就试试
再一次谢谢各位
amandag 2006-06-25
  • 打赏
  • 举报
回复
up
noyester 2006-06-25
  • 打赏
  • 举报
回复
<asp:Repeater runat="server" ID="Rp" EnableViewState="false"><ItemTemplate><table cellspacing="0" border="0" width="100%"><tr><td><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF"><tr><td height="5"></td></tr><tr><td height="20"><table width="98%" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#F1FFF9"><tr><td height="22" class="abc"><a name='<%# DataBinder.Eval(Container, "DataItem.Sing_Index")%>'></a><%# DataBinder.Eval(Container, "DataItem.Sing_Index")%></td></tr></table></td></tr><tr><td height="5"></td></tr></table><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF"><tr><td height="20" colspan="2"><asp:DataList runat="server" ID="DL" EnableViewState="false" RepeatColumns="5" Width="100%" RepeatDirection="Horizontal" CellSpacing=5><ItemTemplate><a href='/<%#Comm.Config.Path_Singer%>/<%#Des_En(DataBinder.Eval(Container, "DataItem.Singer_ID"))%>.html' class="sing"><%#DataBinder.Eval(Container, "DataItem.Singer_Name")%></a></ItemTemplate></asp:DataList></td></tr><tr><td width="90%" height="20" align="right"></td><td width="10%" align="center"><a href="#" target=_self>返回顶部</a></td></tr></table></td></tr><tr><td></td></tr></table></ItemTemplate></asp:Repeater>



Dim alRes As ArrayList
Dim StrErr As String
Dim entity As New entity_T_M_Singer
entity.Type_ID = En_ID
With New Da_T_M_Singer
alRes = .查询指定分类歌手索引列表(entity)
StrErr = .StrError
End With
If Len(StrErr) > 0 Then
Console.WriteLine(StrErr)
WriteLogToText("查询指定分类歌手索引列表错误" & StrErr)
Return
End If
Rp.DataSource = alRes
DataBind()


Protected Sub Rp_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Rp.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim En As entity_T_M_Singer = e.Item.DataItem
Dim DL As DataList = CType(e.Item.FindControl("DL"), DataList)
Dim alRes As ArrayList
Dim StrErr As String
Dim entity As New entity_T_M_Singer
entity.Type_ID = En_ID
entity.Sing_Index = En.Sing_Index
With New DA.Da_T_M_Singer
alRes = .查询指定分类及索引歌手列表(entity)
StrErr = .StrError
End With
DL.DataSource = alRes
DL.DataBind()
End If
End Sub


我一直是这样做的,就是音乐网的歌手列表
lnwuyaowei 2006-06-25
  • 打赏
  • 举报
回复
不好意思.没有认真看,主观以为楼主所言datasource是用了objectdatasource,或者sqldatasource控件.
又看了一下代码才明白........................


汗.................禁言三天.
x03570227 2006-06-25
  • 打赏
  • 举报
回复
嗯,谢谢各位,我马上去试试
我用的系统是vs.net2003+IIS5.0
在我的分页里我用了datasource,而且成功了

现在不太理解lnwuyaowei(风可以追我) 说的,如果你看见了,能不能再给解释下
singlepine 2006-06-25
  • 打赏
  • 举报
回复
http://singlepine.cnblogs.com/archive/2006/04/03/365964.html
gdeljyh 2006-06-25
  • 打赏
  • 举报
回复
看这个datalist示例,repeater可以一样处理

http://www.easewe.com/SoftWare/SoftInfo/DataList%B7%D6%D2%B3%A1%A2%C7%B6%CC%D7%A1%A2%B1%E0%BC%AD%CA%B5%C0%FD.html
xuwenzhuo 2006-06-25
  • 打赏
  • 举报
回复
可以建立这样的对象
class a
{
//属性
}
class b
{
public List<a>
{
get{};
set{};
}
}
邦定的时候b作为主数据,a作为里面的repeat数据,就可以了
lnwuyaowei 2006-06-25
  • 打赏
  • 举报
回复
datasource是2005中才有的,楼主如果不是用2005,不写代码是不能实现这种绑定的.
x03570227 2006-06-25
  • 打赏
  • 举报
回复
谢谢,我试试看先

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧