DataList嵌套DataList

本文展示了一个使用ASP.NET中DataList控件实现的嵌套数据列表示例,该示例从两个数据库表中获取省市数据,并通过设置数据关系实现了嵌套显示。

1.html代码
<HTML>
    
<HEAD>
        
<title>NestedDataLists</title>
        
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        
<meta name="CODE_LANGUAGE" Content="C#">
        
<meta name="vs_defaultClientScript" content="JavaScript">
        
<meta name="vs_targetSchema" content="http://singlepine.cnblogs.com/">
    
</HEAD>
    
<body MS_POSITIONING="GridLayout">
        
<form id="Form1" method="post" runat="server">
            
<asp:DataList runat="server" Id="dlCategories" GridLines="Both" Bordercolor="black" cellpadding="3"
                cellspacing
="0" Headerstyle-BackColor="#DDDDDD" Headerstyle-Forecolor="#777777" Headerstyle-Font-Name="Arial"
                Headerstyle-Font-Size
="14" Headerstyle-Font-Bold="true" Font-Name="Arial" Font-Bold="true"
                Font-Italic
="true" Font-Size="11" ForeColor="Red" RepeatColumns="1">
                
<HeaderTemplate>
                    省市 & 市区
                
</HeaderTemplate>
                
<ItemTemplate>
ExpandedBlockStart.gifContractedBlock.gif                    
<%# DataBinder.Eval(Container, "DataItem.province"%>
                    
<br>
                    
<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">
                        
<ItemTemplate>
                            
&nbsp; &nbsp;
ExpandedBlockStart.gifContractedBlock.gif                            
<%# DataBinder.Eval(Container, "DataItem.city"%>
                        
</ItemTemplate>
                    
</asp:DataList>
                
</ItemTemplate>
            
</asp:DataList>
        
</form>
    
</body>
</HTML>
2.cs代码
public class NestedDataLists : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif    
{
        
protected System.Web.UI.WebControls.DataList dlCategories;
    
        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
string constring=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
            
            DataSet ds
=new DataSet();
            
string sql1="select * from province";
            SqlDataAdapter    sda1 
=new SqlDataAdapter(sql1,constring);
            sda1.Fill(ds,
"province");

            
string sql2="select * from city";
            SqlDataAdapter    sda2 
=new SqlDataAdapter(sql2,constring);
            sda2.Fill(ds,
"city");
            
            ds.Relations.Add(
"myrelation", ds.Tables["province"].Columns["provinceID"], ds.Tables["city"].Columns["father"]);
            dlCategories.DataSource
=ds.Tables["province"].DefaultView;
            dlCategories.DataBind();
        }

        
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web Form Designer generated code#region Web Form Designer generated code
        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//
            
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
            
//
            InitializeComponent();
            
base.OnInit(e);
        }

        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// Required method for Designer support - do not modify
        
/// the contents of this method with the code editor.
        
/// </summary>

        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{    
            
this.Load += new System.EventHandler(this.Page_Load);

        }

        
#endregion

    }

转载于:https://www.cnblogs.com/JemBai/archive/2008/09/13/1290660.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值