repeater控件动态数据绑定使用

通常用于效率优先的使用中代替gridview

 ASPX代码

<asp:Repeater ID="rptCategory" runat="server" OnItemDataBound="rptCategory_ItemDataBound">
                <HeaderTemplate>
                    <table width="778" border="0" align="center" cellpadding="0" cellspacing="0" id="table1">
                </HeaderTemplate>
                <ItemTemplate>
                    <tr bgcolor="#EFEEEE">
                        <td align="left" valign="top">
                            <asp:Label ID="lblCategory" runat="server" Text="Label"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td align="left" valign="top">
                            <asp:Repeater ID="rptNewsList" runat="server" OnItemDataBound="rptNewsList_ItemDataBound">
                                <HeaderTemplate>
                                    <table width="778" border="0" align="center" cellpadding="0" cellspacing="0" id="table1">
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <tr>
                                        
                                        <asp:HyperLink runat="server" ID="hlkNewsTitle">HyperLink</asp:HyperLink>
                                        
                                    </tr>
                                </ItemTemplate>
                                <FooterTemplate>
                                    </table>
                                </FooterTemplate>
                            </asp:Repeater>
                        </td>
                    </tr>
                </ItemTemplate>
                
                <AlternatingItemTemplate>
                    <tr bgcolor="#DEFCCF">
                        <td align="left" valign="top">
                            <asp:Label ID="lblCategory" runat="server" Text="Label"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td align="left" valign="top">
                            <asp:Repeater ID="rptNewsList" runat="server" OnItemDataBound="rptNewsList_ItemDataBound">
                                <HeaderTemplate>
                                    <table width="778" border="0" align="center" cellpadding="0" cellspacing="0" id="table1">
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <tr>
                                        <asp:HyperLink runat="server" ID="hlkNewsTitle">HyperLink</asp:HyperLink>
                                    </tr>
                                </ItemTemplate>
                                <FooterTemplate>
                                    </table>
                                </FooterTemplate>
                            </asp:Repeater>
                        </td>
                    </tr>
                </AlternatingItemTemplate>
                <FooterTemplate>
                    </table>
                </FooterTemplate>
            </asp:Repeater>


 CS代码

 using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using News.BLL;
using News.Model;

public partial class Index : System.Web.UI.Page
{
    News.BLL.Category cate = new Category();
    News.BLL.Newses news = new Newses();
    protected void Page_Load(object sender, EventArgs e)
    {
        InitControl();
        DataBind();
       
    }

    private void InitControl()
    {
        rptCategory.DataSource = cate.GetCategorys();
        rptCategory.DataMember = "CategoryInfo";
    }


    protected void rptCategory_ItemDataBound(object sender, RepeaterItemEventArgs e)

    {
       //检查当前绑定项是否为所指定类
        CategoryInfo info = (CategoryInfo)e.Item.DataItem;
        if (info != null)
        {
            //查找并绑定数据到指定控件
            Label lblCate = (Label)e.Item.FindControl("lblCategory");
            lblCate.Text = info.CategoryName;
            lblCate.Font.Bold = true;
            lblCate.Font.Name = "宋体";

            //查找嵌套repeater控件并对其加载数据源执行数据绑定
            Repeater rptNewsList = (Repeater)e.Item.FindControl("rptNewsList");
            if (rptNewsList != null)
            {
                rptNewsList.DataSource = news.GetNewsesByCategoryId(info.CategoryId);
                rptNewsList.DataBind();
            }
   
        }

    }
    protected void rptNewsList_ItemDataBound(object sender, RepeaterItemEventArgs e)

    {
    //检查当前绑定项是否为所指定类
        NewsInfo info = (NewsInfo)e.Item.DataItem;
        if (info != null)
        {
            //查找并绑定数据到指定控件
            HyperLink hlkNewsTitle = e.Item.FindControl("hlkNewsTitle") as HyperLink;
            if (hlkNewsTitle != null)
            {
                hlkNewsTitle.Text = info.TitleName+"[发布时间:"+info.PublishTime+"]";
                hlkNewsTitle.Font.Bold = false;
                hlkNewsTitle.Font.Size = 11;
                hlkNewsTitle.NavigateUrl = "NewsContent.aspx?newsid=" + info.NewsId;
            }     
        }
    }
}

posted @ 2008-11-14 01:49  Edward Xie  阅读(3810)  评论(0编辑  收藏  举报