ASP.NET动态创建DATALIST

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using Starock.BLL;
using Starock.Model;
using Starock.Comm;

public partial class style_inPage : System.Web.UI.Page
{  

    BLL_StyleClass myStyleClass = new BLL_StyleClass();
    BLL_SytleInfo myStyleInfo = new BLL_SytleInfo();

    Comm myComm = new Comm(); 


    static int iClass = 0;
    static int iPage = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request["class"] != null)
            {
                iClass = Convert.ToInt32(Request["class"].ToString());
            }

            ShowList(iClass, iPage);
                       
        }
    }

  


    public void ShowList(int iClass, int iPage)
    {
        lTest.Text = iClass.ToString();
      

        DataList dlInfo = new DataList();

        dlInfo.ID = "dlInfo";
        dlInfo.DataKeyField = "ID";
        dlInfo.Width = 590;
        dlInfo.RepeatColumns = 4; //一行4列
        dlInfo.RepeatDirection = RepeatDirection.Horizontal; //横向显示

        dlInfo.ItemDataBound += new DataListItemEventHandler(dlInfo_ItemDataBound); //添加数据绑定事件

        dlInfo.ItemTemplate = new CMyTemplate("lItem"); //创建模板列,通过构造函数初始化控件名称

        dlInfo.DataSource = myStyleInfo.GetList(iClass);
        dlInfo.DataBind();

        //this.Controls.Add(dlInfo); //将DATALIST添加到当前页面中(这里因为要返回HTML代码,所以屏蔽该句)


        //把DataList输出成HTML
        Response.Write(this.GetStringByControl(dlInfo));
    }


    private void dlInfo_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
    {
        Label lItem;
        int ID = 0;

        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            lItem = (Label)e.Item.FindControl("lItem");
            if (lItem != null)
            {               
                ID = Convert.ToInt32(((DataRowView)e.Item.DataItem).Row["ID"].ToString());

                lItem.Text = "<table width='146' border='0' cellspacing='0' cellpadding='0'><tr>" +
                             "<td><div align='center'><a href='./images/style/" + GetPhotoBig(ID) + "' target='_blank' title='"+GetInfo(ID).StyleName +"' rel='lightbox[roadtrip]'><img src='./images/style/"+GetPhoto(ID)+"' width='125' height='125' border='0' /></a></div></td>" +
                             "</tr><tr><td>&nbsp;</td></tr></table>";
            }
        }

    }

 


    public string GetStringByControl(System.Web.UI.Control theControl)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        System.IO.StringWriter writer = new System.IO.StringWriter(sb);
        System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(writer);
        theControl.RenderControl(htw);

        return sb.ToString();
    }


    public string GetPhoto(int ID)
    {
        StyleInfo oInfo = myStyleInfo.GetInfo(ID);

        if (oInfo.Photo != null)
        {
            return oInfo.Photo;
        }
        else
        {
            return "null.jpg";
        }
    }


    public string GetPhotoBig(int ID)
    {
        StyleInfo oInfo = myStyleInfo.GetInfo(ID);

        if (oInfo.Photo_Big != null)
        {
            return oInfo.Photo_Big;
        }
        else
        {
            return "null_big.jpg";
        }
    }


    public StyleInfo GetInfo(int ID)
    {
        return myStyleInfo.GetInfo(ID);
    }


}

 


class CMyTemplate : ITemplate
{
    private string pControlName; //控件名称  

    public CMyTemplate(string ControlName)
    {
        pControlName = ControlName;     
    }

    public void InstantiateIn(Control container)
    {
        Label lItem = new Label();
        lItem.ID = pControlName;
        container.Controls.Add(lItem);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值