repeater 嵌套的问题
两个Repeater嵌套内层的Repeater想要输出属于外层Repeater的数据怎么输出?代码如下:<asp:RepeaterID="ParentRepeater"r...
两个Repeater嵌套 内层的Repeater想要输出属于外层Repeater的数据 怎么输出?
代码如下:
<asp:Repeater ID="ParentRepeater" runat="server" OnItemDataBound="CRDB_ItemDataBound">
<ItemTemplate>
<div style=" padding-left:10px; text-align:left"><%# Eval("ClassName") %></div>
<asp:Repeater ID="ChildRepeater" runat="server">
<ItemTemplate>
<div style=" padding-left:30px; text-align:left"><%# Eval("ClassName") %> 父类丝毫:<%# ParentRepeater.Eval("ClassName") %></div>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
错误之处:父类丝毫:<%# ParentRepeater.Eval("ClassName") %>
08.Net年[1009604112] 2009-04-25 16:37:14
外层的Repenter绑定了表1
08.Net年[1009604112] 2009-04-25 16:37:15
内层的Repeater根据表1的ClassID 绑定表2
08.Net年[1009604112] 2009-04-25 16:37:26
内层Repeater的某个地方需要输出表1的字段Classname
08.Net年[1009604112] 2009-04-25 16:37:34
表1和表2的字段名相同 都有ClassName 如果内层Repeater要用<%# Eval("ClassName")%>的话 他又如何知道是要提取表一的ClassName呢 还是表二的ClassName呢 呵呵 展开
代码如下:
<asp:Repeater ID="ParentRepeater" runat="server" OnItemDataBound="CRDB_ItemDataBound">
<ItemTemplate>
<div style=" padding-left:10px; text-align:left"><%# Eval("ClassName") %></div>
<asp:Repeater ID="ChildRepeater" runat="server">
<ItemTemplate>
<div style=" padding-left:30px; text-align:left"><%# Eval("ClassName") %> 父类丝毫:<%# ParentRepeater.Eval("ClassName") %></div>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
错误之处:父类丝毫:<%# ParentRepeater.Eval("ClassName") %>
08.Net年[1009604112] 2009-04-25 16:37:14
外层的Repenter绑定了表1
08.Net年[1009604112] 2009-04-25 16:37:15
内层的Repeater根据表1的ClassID 绑定表2
08.Net年[1009604112] 2009-04-25 16:37:26
内层Repeater的某个地方需要输出表1的字段Classname
08.Net年[1009604112] 2009-04-25 16:37:34
表1和表2的字段名相同 都有ClassName 如果内层Repeater要用<%# Eval("ClassName")%>的话 他又如何知道是要提取表一的ClassName呢 还是表二的ClassName呢 呵呵 展开
1个回答
展开全部
1.在Page_Load事件中蔽消添加外层Repeater的ItemDataBound事件,并绑定数据源
ParentRepeater.ItemDataBound += new RepeaterItemEventHandler(ParentRepeater_ItemDataBound);
DataTable table = GetParentData();
if (table != null)
{
ParentRepeater.DataSource = table.DefaultView;
ParentRepeater.DataBind();
}
2.在外层Repeater(ParentRepeater)的ItemDataBound事件中绑定内层Repeater即可:
protected void ParentRepeater_ItemDataBound(object obj, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView row = (DataRowView)e.Item.DataItem;
Repeater child = (Repeater)e.Item.FindControl("ChildRepeater");
//此处宏高知可以获取表一的ClassName
string className = row["ClassName"].ToString()
//获念戚取父ID
int parentClassID = row["ClassID"].ToString();
//根据父ID读取跟父ID关联的数据绑定到内部Repeater
DataTable table = GetChidData(parentClassID);
if (table != null)
{
child.DataSource = table.DefaultView;
child.DataBind();
}
}
}
ParentRepeater.ItemDataBound += new RepeaterItemEventHandler(ParentRepeater_ItemDataBound);
DataTable table = GetParentData();
if (table != null)
{
ParentRepeater.DataSource = table.DefaultView;
ParentRepeater.DataBind();
}
2.在外层Repeater(ParentRepeater)的ItemDataBound事件中绑定内层Repeater即可:
protected void ParentRepeater_ItemDataBound(object obj, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView row = (DataRowView)e.Item.DataItem;
Repeater child = (Repeater)e.Item.FindControl("ChildRepeater");
//此处宏高知可以获取表一的ClassName
string className = row["ClassName"].ToString()
//获念戚取父ID
int parentClassID = row["ClassID"].ToString();
//根据父ID读取跟父ID关联的数据绑定到内部Repeater
DataTable table = GetChidData(parentClassID);
if (table != null)
{
child.DataSource = table.DefaultView;
child.DataBind();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询