再一次发贴,关于repeater嵌套repeater的问题

x03570227 2006-06-26 05:25:05
昨天已经发过一次了,虽然大家给了我一些解决方法,但最终还是没有成功,可能是由于我菜吧
今天仔细地翻了一遍百度,csdn,msdn,以及其它一些论坛
看了些文章,
其中有两篇:http://support.microsoft.com/kb/326338/EN-US/#kb2,http://www.ymw.cn/it/CSDN/asppost1/web14028.htm,感觉这种方法不错,所以就用了这种办法,连接什么的都对了,但最终于还是有些问题
我先把代码贴一下,不然真有点难说
前台代码:
<asp:Repeater id="Repeater1" runat="server">
<ItemTemplate>
<ul>
<%# DataBinder.Eval(Container.DataItem, "sortid") %>
<%# DataBinder.Eval(Container.DataItem, "sortName") %>
<asp:Repeater ID="Rpin" Runat="server" datasource='<%# ((DataRow)Container.DataItem).GetChildRows("myrl") %>'>
<ItemTemplate>
<li>
<%#DataBinder.Eval(Container.DataItem,"Logname")%>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
</ItemTemplate>
</asp:Repeater>
后台代码:
public class RpInRp : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Repeater Repeater1;

private void Page_Load(object sender, System.EventArgs e)
{
SqlConnection myconn = new SqlConnection("Server=localhost;Database=web%5cnet;User ID=sa;Password=;Trusted_Connection=False");
SqlCommand mycmd=new SqlCommand();
SqlDataAdapter mydt=new SqlDataAdapter();
DataSet myds = new DataSet();

mycmd.Connection=myconn;
mycmd.Connection.Open();
mycmd.CommandText="select * from logsort";
mydt.SelectCommand=mycmd;
mydt.Fill(myds,"logsort");

SqlCommand mycmd2=new SqlCommand();
SqlDataAdapter mydt2 = new SqlDataAdapter("select * from loginfo ",myconn);
mydt2.Fill(myds,"loginfo");
myds.Relations.Add("myrl",myds.Tables["logsort"].Columns["sortid"],myds.Tables["LogInfo"].Columns["sortid"]);
Repeater1.DataSource = myds.Tables["logsort"].DefaultView;
Repeater1.DataBind();
Page.DataBind();
DataGrid1.DataSource = myds.Tables["logsort"].DefaultView;
DataGrid1.DataBind();

myconn.Close();

}
报错:指定的转换无效。
行 20: <asp:Repeater ID="Rpin" Runat="server" datasource='<%# ((DataRow)Container.DataItem).GetChildRows("myrl") %>'>

曾经偿试:
1.
用Container.DataItem.Row.GetChildRows("myrelation")时
报错:“object”并不包含对“Row”的定义
行 20: <asp:Repeater ID="Rpin" Runat="server" datasource='<%# Container.DataItem.Row.GetChildRows("myrelation") %>'>

2.
用(Container.DataItem,DataRow).GetChildRows("myrelation2")时
报错:CS1026: 应输入 )
行 20: <asp:Repeater ID="Rpin" Runat="server" datasource='<%#(Container.DataItem,DataRow).GetChildRows("myrelation2") %>'>

3.
用((DataRow)Container.DataItem).GetChildRows("myrelation2") 时
报错:CS1026: 指定的转换无效。
行 20:<asp:Repeater ID="Rpin" Runat="server" datasource='<%# ((DataRow)Container.DataItem).GetChildRows("myrelation2") %>'>

4.
用((DataRow)Container.DataItem).GetChildRows("myrelation2") 时
报错:CS1026: 指定的转换无效。
行 20:<asp:Repeater ID="Rpin" Runat="server" datasource='<%# ((DataRow)Container.DataItem).GetChildRows("myrelation2") %>'>

实在是想不出办法了,希望大家能帮帮我,谢谢了
...全文
160 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
x03570227 2006-06-28
  • 打赏
  • 举报
回复
不对,搞错了,是这个帖子
<%#DataBinder.Eval(Container.DataItem,"Logname")%>这句话错了,应该换成
<%#DataBinder.Eval(Container.DataItem,"[\"Logname\"]")%>

谢谢各位csdn朋友的帮忙
x03570227 2006-06-28
  • 打赏
  • 举报
回复
问题解决了,换了另一种方法,在另一个贴子里,如果有需要可以去找那个帖子
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);
}
}
}
x03570227 2006-06-26
  • 打赏
  • 举报
回复
我已经加了<%@ Import Namespace="System.Data" %>和<%@ Import Namespace="System.Data.SqlClient" %>命名空间,数据库里我敢保证有loginfo跟logsort两张表,并且有sortid这个字段
amandag 2006-06-26
  • 打赏
  • 举报
回复
up
孟子E章 2006-06-26
  • 打赏
  • 举报
回复
pubs为sql 2000自带的数据库,修改数据库连接即可运行
孟子E章 2006-06-26
  • 打赏
  • 举报
回复
aspx里
<%@ Import Namespace="System.Data" %>
必须
孟子E章 2006-06-26
  • 打赏
  • 举报
回复
可以运行的例子
http://dotnet.aspx.cc/ShowDetail.aspx?id=54F4C732-AAE2-4135-FB1B-7B4B613BAA33

62,074

社区成员

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

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

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

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