02
2008
04

Repeart控件中嵌套DataList控件

Repeart控件中嵌套DataList控件,一般显示数据,网页上都比较喜欢用Datalist因为他很灵活,而且样式好控制.而一些特定的功能需要用到数据控件之间绑定才能实现.但如果在DATALIST中再绑定一个DATALIST呢?这样效率很底.在功能条件允许的情况下,应该更优先的考虑REPERT很DATALIST的嵌套.

下面是我写的一个Repeart控件中嵌套DataList控件例子

test.aspx

    <div>
        <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
        <ItemTemplate>
            <li style="color:Blue;"><%#DataBinder.Eval(Container.DataItem,"name") %></li>
            <asp:DataList ID="DataList1" runat="server">
            <ItemTemplate>
                 <li style="padding-left:30px; color:Red;"><%#DataBinder.Eval(Container.DataItem,"title") %></li>
            </ItemTemplate>
            </asp:DataList>
        </ItemTemplate>
        </asp:Repeater>
    </div>

test.aspx.cs

    protected void Page_Load(object sender, EventArgs e)
    {
        string sql="select * from nvwa_type order by id";
        string connstring="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("~/App_Data/mydate.mdb");
        OleDbDataAdapter oda= new OleDbDataAdapter(sql,connstring);
        DataSet ds=new DataSet();
        oda.Fill(ds);
        this.Repeater1.DataSource = ds;
        this.Repeater1.DataBind();
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            string connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/App_Data/mydate.mdb");

            int type_id = Convert.ToInt32(((DataRowView)e.Item.DataItem).Row["id"].ToString());

            DataList dl = (DataList)e.Item.FindControl("DataList1"); //找到DataList1控件
            if (type_id != 0 && type_id != null)
            {
                string sql_p = "select * from nvwa_product where type=" + type_id + " order by id";
                OleDbDataAdapter oda = new OleDbDataAdapter(sql_p, connstring);
                DataSet ds = new DataSet();
                oda.Fill(ds);
                dl.DataSource = ds;
                dl.DataBind();
            }

        }
    }

 

« 上一篇下一篇 »

评论列表:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。