现在的位置: 首页 > 综合 > 正文

用DataList实现一对多的关系

2011年04月23日 ⁄ 综合 ⁄ 共 4325字 ⁄ 字号 评论关闭

以前用DataGrid实现父子关系
父表ClassName

对应父表的ClassID的文章

但DataGrid好像不能并列排版,后来改用DataList
ChannelNewsArticleList.ascx

<%@ Control Language="c#" AutoEventWireup="false" Codebehind="ChannelNewsArticleList.ascx.cs" Inherits="Hover.Web.Article.Ascx.ChannelNewsArticleList" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<asp:DataList id="DataList1" runat="server" RepeatColumns="2" RepeatDirection="Horizontal">
    
<ItemTemplate>
        
<TABLE width="278" border="0" valign="top" cellPadding="0" cellSpacing="0" >
            
<TR>
                
<TD align="top" width="278" background="images/wu.gif" height="40">
                
<table width="100%" border="0" cellspacing="0" cellpadding="0">
                        
<tr>
                            
<td width="19%">&nbsp;</td>
                            
<td width="81%"><font color="#000000" size=5><%# DataBinder.Eval(Container.DataItem,"classname")%></font></td>
                        
</tr>
                    
</table>
                
</TD>
                
<TD width="1" background="images/xu.gif"></TD>
            
</TR>
            
<TR>
                
<TD height="200">&nbsp;<IMG src="images/none.gif" border="0"><IMG src="images/title.gif" border="0">
                    
<asp:DataList ID=DataList2 runat=server DataSource='<%# FillDataList2()%>'>
                        
<ItemTemplate>
                            
<%# DataBinder.Eval(Container.DataItem,"ArticleTitle"%>
                        
</ItemTemplate>
                    
</asp:DataList>
                
</TD>
                
<TD width="1" background="images/xu.gif"></TD>
            
</TR>
        
</TABLE>
    
</ItemTemplate>
</asp:DataList>

ChannelNewsArticleList.ascx.cs

namespace Hover.Web.Article.Ascx
{
    
using System;
    
using System.Data;
    
using System.Drawing;
    
using System.Web;
    
using System.Web.UI.WebControls;
    
using System.Web.UI.HtmlControls;

    
/// <summary>
    
///        ChannelNewsArticleList 的摘要说明。
    
/// </summary>

    public class ChannelNewsArticleList : System.Web.UI.UserControl
    
{
        Hover.BusinessLayer.Article.ArticleBL obj
=new Hover.BusinessLayer.Article.ArticleBL();
        
protected System.Web.UI.WebControls.DataList DataList1;
        
protected System.Web.UI.WebControls.Label Label1;
        
private int ChannelIndex;
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面
            this.ShowNewArticleRelation();
            
        }

        
/// <summary>
        
/// 获得父表
        
/// </summary>

        public void ShowNewArticleRelation()
        
{
            DataSet ds
=new DataSet();
            ds
=obj.GetAllClass();                    
            
this.DataList1.DataSource=ds;
            
this.DataList1.DataKeyField="ClassID";
            
this.DataList1.DataBind();        

        }

        
    
        
/// <summary>
        
/// 相对应的子表文章数据集供DataList2绑定数据源
        
/// </summary>
        
/// <returns></returns>

        public DataSet FillDataList2()
        
{
            DataSet ds
=new DataSet();
            ds
=obj.GetArticleByClassID((int)this.DataList1.DataKeys[ChannelIndex]);
            
return ds;
        }


        
Web 窗体设计器生成的代码
        
/// <summary>
        
/// 获得父表的ClassID索引
        
/// 供子表根据ClassID读出相对应分类的文章
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>

        private void DataList1_ItemCreated(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
        
{
            
if(e.Item.ItemType==ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)
            
{
                
this.ChannelIndex=e.Item.ItemIndex;
            }

            
        }

    }

}

参考了Erist.Protal站点程序

效果参看http://bns.23city.com/
首页的asp.net和csahrp分类及分类下的文章。

抱歉!评论已关闭.