如何做嵌套模版的数据绑定?(可以再加分)

popcorn 2002-10-25 10:46:22
有一个模版控件了,可以绑定一个数据源,我想在其中再加入一个模版显示另外一个数据源,运行的结果却总是错误。
比如说,有一个客户资料表和一个定单表,每个客户有多个定单,我想象下面那样把所有资料显示出来该怎么做?有没有类似的例子?

客户名称1
定单1 定单内容1
定单2 定单内容2
客户名称2
定单3 定单内容3
定单4 定单内容4
定单5 定单内容5
客户名称3
定单6 定单内容6
....
...全文
61 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
popcorn 2002-10-27
  • 打赏
  • 举报
回复
好好好,问题解决,多谢多谢!
saucer 2002-10-26
  • 打赏
  • 举报
回复
since you already have " inside, you need to use ':

<asp:repeater id="child" datasource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("myrelation") %>' runat="server">
popcorn 2002-10-26
  • 打赏
  • 举报
回复
谢谢saucer(思归),
DataGrid有OnItemDataBound事件可以在创建时绑定子表,
有没有使用repeater的例子?
下面这段代码可以绑定子表,但是我运行时总是说服务器标记错误,错误在这一行:
datasource="<%# ((DataRowView)Container.DataItem)
.Row.GetChildRows("myrelation") %>" runat="server">
能否帮我看看是哪里错了。

Nestedrepeater.aspx
<%@ Page Language=C# Inherits="yourprojectname.nestedrepeater" %>
<%@ Import Namespace="System.Data" %>
<html>
<body>
<form runat=server>
<!-- start parent repeater -->
<asp:repeater id="parent" runat="server">
<itemtemplate>
<b><%# DataBinder.Eval(Container.DataItem,"au_id") %></b><br>
<!-- start child repeater -->
<asp:repeater id="child" 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>
Nestedrepeater.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 yourprojectname
{
public class nestedrepeater : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Repeater parent;
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=(local);database=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.
parent.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);
}
}
}


saucer 2002-10-26
  • 打赏
  • 举报
回复
take a look at these examples to display Master/Detail information

HOW TO: Create a Master/Detail Page with Web Form Controls
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q308485

Vanishing Master/Detail DataGrid
http://www.dotnetjunkies.net/tutorials.aspx?tutorialid=387

or play with this simple nested datagrids:

<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<html>

<body>

<h3><font face="Verdana">Nested Grid</font></h3>
<form runat="server">
<ASP:DataGrid id="MyDataGrid" runat="server"
Width="700"
ForeColor="#ff0000"
BackColor=""
BorderColor="black"
ShowFooter="false"
CellPadding=3
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
EnableViewState="false"
OnItemDataBound="Customer_OnItemDataBound"
/>

</form>

</body>
</html>


<script language="C#" runat="server">

SqlConnection myConnection;

protected void Page_Load(Object sender, EventArgs e)
{
myConnection = new SqlConnection("server=localhost;database=NorthWind;uid=sa;pwd=;");
SqlDataAdapter myCommand = new SqlDataAdapter("select CustomerID, CompanyName ContactName from Customers", myConnection);

DataSet ds = new DataSet();
myCommand.Fill(ds, "Customers");

DataView dv = ds.Tables["Customers"].DefaultView;
MyDataGrid.DataSource= dv;

MyDataGrid.DataBind();

}


protected void Customer_OnItemDataBound(object sender, DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
String CustomerID = DataBinder.Eval(e.Item.DataItem, "CustomerID").ToString();

DataGrid dg = new DataGrid();

SqlDataAdapter myCommand = new SqlDataAdapter("select OrderID, OrderDate from orders WHERE CustomerID = '" + CustomerID + "'" , myConnection);

DataSet ds = new DataSet();
myCommand.Fill(ds, "Orders");

DataView dv = ds.Tables["Orders"].DefaultView;
dg.DataSource= dv;
dg.DataBind();

TableCell cell = new TableCell();
cell.Controls.Add(dg);
e.Item.Cells.Add(cell);
}
}

</script>
popcorn 2002-10-25
  • 打赏
  • 举报
回复
有没有例子呢,多谢?
龙腾九霄 2002-10-25
  • 打赏
  • 举报
回复
应该从数据库取出的时候就作处理。
然后再databind的时候过滤掉重复的数据。。

62,249

社区成员

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

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

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

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