来一起疯(617feng)

 DataList嵌套DataList

Fanjf, 发表于:2010-06-22 10:41:49, 分类:ASP.net学习 浏览( ) 评论( )  收藏这篇日志

添加到网摘:


1.html代码
 

csharp 代码
 
  1. <HTML>
  2.     <HEAD>
  3.         <title>NestedDataLists</title>
  4.         <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
  5.         <meta name="CODE_LANGUAGE" Content="C#">
  6.         <meta name="vs_defaultClientScript" content="JavaScript">
  7.         <meta name="vs_targetSchema" content="http://singlepine.cnblogs.com/">
  8.     </HEAD>
  9.     <body MS_POSITIONING="GridLayout">
  10.         <form id="Form1" method="post" runat="server">
  11.             <asp:DataList runat="server" Id="dlCategories" GridLines="Both" Bordercolor="black" cellpadding="3"
  12.                 cellspacing="0" Headerstyle-BackColor="#DDDDDD" Headerstyle-Forecolor="#777777" Headerstyle-Font-Name="Arial"
  13.                 Headerstyle-Font-Size="14" Headerstyle-Font-Bold="true" Font-Name="Arial" Font-Bold="true"
  14.                 Font-Italic="true" Font-Size="11" ForeColor="Red" RepeatColumns="1">
  15.                 <HeaderTemplate>
  16.                     省市 & 市区
  17.                 </HeaderTemplate>
  18.                 <ItemTemplate>
  19.                     <%# DataBinder.Eval(Container, "DataItem.province") %>
  20.                     <br>
  21.                     <asp:DataList runat="server" Id="ChildDataList" GridLines="None" Bordercolor="black" cellpadding="3" cellspacing="0" Headerstyle-BackColor="#8080C0" Headerstyle-Font-Name="Arial" Headerstyle-Font-Size="8" Font-Name="Arial" Font-Size="8" datasource='<%# DataBinder.Eval(Container, "DataItem.myrelation") %>' RepeatColumns="5">
  22.                         <ItemTemplate>
  23.                                
  24.                             <%# DataBinder.Eval(Container, "DataItem.city") %>
  25.                         </ItemTemplate>
  26.                     </asp:DataList>
  27.                 </ItemTemplate>
  28.             </asp:DataList>
  29.         </form>
  30.     </body>
  31. </HTML>

2.cs代码

csharp 代码
 
  1. public class NestedDataLists : System.Web.UI.Page
  2.     {
  3.         protected System.Web.UI.WebControls.DataList dlCategories;
  4.     
  5.         private void Page_Load(object sender, System.EventArgs e)
  6.         {
  7.             string constring=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
  8.             
  9.             DataSet ds=new DataSet();
  10.             string sql1="select * from province";
  11.             SqlDataAdapter    sda1 =new SqlDataAdapter(sql1,constring);
  12.             sda1.Fill(ds,"province");
  13.  
  14.             string sql2="select * from city";
  15.             SqlDataAdapter    sda2 =new SqlDataAdapter(sql2,constring);
  16.             sda2.Fill(ds,"city");
  17.             
  18.             ds.Relations.Add("myrelation", ds.Tables["province"].Columns["provinceID"], ds.Tables["city"].Columns["father"]);
  19.             dlCategories.DataSource=ds.Tables["province"].DefaultView;
  20.             dlCategories.DataBind();
  21.         }
  22.         
  23.         Web Form Designer generated code#region Web Form Designer generated code
  24.         override protected void OnInit(EventArgs e)
  25.         {
  26.             //
  27.             // CODEGEN: This call is required by the ASP.NET Web Form Designer.
  28.             //
  29.             InitializeComponent();
  30.             base.OnInit(e);
  31.         }
  32.         
  33.         /**//// <summary>
  34.         /// Required method for Designer support - do not modify
  35.         /// the contents of this method with the code editor.
  36.         /// </summary>
  37.         private void InitializeComponent()
  38.         {    
  39.             this.Load += new System.EventHandler(this.Page_Load);
  40.  
  41.         }
  42.         #endregion
  43.     }

3.演示数据库下載:/Files/singlepine/area1.rar

转自:http://www.cnblogs.com/singlepine/archive/2006/04/03/365964.html

另附:关于“不能启用此约束,因为不是所有的值都具有相应的父值”的解决方法

 

正在读取日志的评论数据,请稍后……
正在加载日志评论签写框,请稍后……
成员登录通道
正在载入成员登录通道...
BLOG 日历助手
正在载入日历助手...
Feed订阅
feedsky
抓虾 google reader
my yahoo 鲜果
哪吒 有道
BLOG 统计信息
正在载入统计信息...
BLOG 分类列表
BLOG 日志归档
BLOG 最新评论
{$SideComment}
BLOG 最新留言
{$SideGB}
BLOG 站内搜索

BLOG 友情链接
来访地区,浏览器
百度广告