前往顾页
以后地位: 主页 > 收集编程 > .Net实例教程 >

实现嵌套的数据控件在子控件绑定时拜候父控件的数据绑定项的体例

时候:2011-06-02 23:40来源:知行网www.zhixing123.cn 编辑:麦田守望者

ASPX 代码
 

<%@ Page Language="C#" EnableViewState="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
System.Data.DataSet ds = new System.Data.DataSet();
System.Data.DataTable dataTable1 = new System.Data.DataTable("BlogUser");
System.Data.DataRow dr;
dataTable1.Columns.Add(new System.Data.DataColumn("UserId", typeof(System.Int32)));
dataTable1.Columns.Add(new System.Data.DataColumn("UserName", typeof(System.String)));
dataTable1.PrimaryKey = new System.Data.DataColumn[] { dataTable1.Columns["UserId"] };
for (int i = 0; i < 8; i++)
{
dr = dataTable1.NewRow();
dr[0] = i;
dr[1] = "用户姓名【孟子E章】测试" + i.ToString();
dataTable1.Rows.Add(dr);
}
System.Data.DataTable dataTable2 = new System.Data.DataTable("BlogArticle");
dataTable2.Columns.Add(new System.Data.DataColumn("ArticleId", typeof(System.Int32)));
dataTable2.Columns.Add(new System.Data.DataColumn("Title", typeof(System.String)));
dataTable2.Columns.Add(new System.Data.DataColumn("UserId", typeof(System.Int32)));
dataTable2.PrimaryKey = new System.Data.DataColumn[] { dataTable1.Columns["ArticleId"] };
Random rd = new Random();
for (int i = 0; i < 20; i++)
{
dr = dataTable2.NewRow();
dr[0] = i;
dr[1] = "文章题目例子" + i.ToString();
dr[2] = rd.Next(0, 7);
dataTable2.Rows.Add(dr);
}
ds.Tables.Add(dataTable1);
ds.Tables.Add(dataTable2);
System.Data.DataColumn parentColumn = ds.Tables["BlogUser"].Columns["UserId"];
System.Data.DataColumn childColumn = ds.Tables["BlogArticle"].Columns["UserId"];
System.Data.DataRelation user_article = new System.Data.DataRelation("UserId", parentColumn, childColumn);
ds.Relations.Add(user_article);
this.parentRepeater.DataSource = ds.Tables["BlogUser"].DefaultView;
this.parentRepeater.DataBind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<form id="form1" runat="server">
<!-- 父Repeater开端 -->
<asp:Repeater ID="parentRepeater" runat="server">
<HeaderTemplate><table border="1"></HeaderTemplate>
<FooterTemplate></table></FooterTemplate>
<ItemTemplate>
<tr style="text-align:left">
<th style="background-color:#EEEEEE"><input type="checkbox" name="cb_<%# Container.ItemIndex + 1 %>" />
<%# DataBinder.Eval(Container.DataItem, "UserName")%>
</th>
</tr>
<ul>
<!-- 子Repeater开端 -->
<asp:Repeater ID="childRepeater" runat="server"
DataSource='<%# ((System.Data.DataRowView)Container.DataItem).Row.GetChildRows("UserId") %>'>
<ItemTemplate>
<tr>
<td style="padding-left:20px">
<input type="checkbox" name="cb_<%# ((RepeaterItem)Container.Parent.Parent).ItemIndex %>_<%# Container.ItemIndex%>" />
<%# DataBinder.Eval(Container.DataItem, "[\"Title\"]")%>
父级数据:<%# DataBinder.Eval(((RepeaterItem)Container.Parent.Parent).DataItem, "UserName")%>
父级序号:<%# ((RepeaterItem)Container.Parent.Parent).ItemIndex %>
</td>
</ItemTemplate>
</asp:Repeater>
</tr>
<!-- 子Repeater结束 -->
</ItemTemplate>
</asp:Repeater>
<!-- 父Repeater结束 -->
</form>
</body>
</html>
 

顶一下
(1)
100%
踩一下
(0)
0%
------分开线----------------------------
标签(Tag):C# C#实例教程 c#根本教程 C#源代码
------分开线----------------------------
颁发评论
请自发遵循互联网相关的政策法规,严禁公布色情、暴力、革命的谈吐。
评价:
神色:
考证码:点击我更换图片
猜你感兴趣