如何动态创建数据列表 [英] How To Create Dynamically datalist

查看:107
本文介绍了如何动态创建数据列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

Here is my Code

DataList dl = new DataList();
dl.DataSource = from n in context.Projects  select n;//Data from DB 
dl.DataBind();
panel1.Controls.Add(dl);



在此先感谢

代码格式为



Thanks in Advance

Code formatted

推荐答案


试试这个会引导您

Hi ,
Try this will Guide you

    DataClassesDataContext db = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var result = from x in db.Items
                         select x;

            DataList DataList1 = new DataList();
            DataList1.ItemTemplate = new DatalistLabelColumnBind();
            DataList1.DataSource = result;
            DataList1.DataBind();
            form1.Controls.Add(DataList1); 
        }
    }
//Create a new class implementing ITemplate
public class DatalistLabelColumnBind : ITemplate
{
    public DatalistLabelColumnBind()
	{
		//Add constructor 
	}
    public void InstantiateIn(Control container)
    {
        Label label1 = new Label();
        label1.DataBinding += new EventHandler(this.BindLabelColumn);
        container.Controls.Add(label1);
    }
	public void BindLabelColumn(object sender, EventArgs e)
	{
		Label lbl = (Label)sender;
		DataListItem  container = (DataListItem)lbl.NamingContainer ;
        String strVals = Convert.ToString(DataBinder.Eval(((DataListItem)container).DataItem, "item_code")) 
			+ " <br />" +
            Convert.ToString(DataBinder.Eval(((DataListItem)container).DataItem, "Item_name")) 
			;
		lbl.Text  = strVals;
		
	}
}


最好的问候
M.Mitwalli


Best Regards
M.Mitwalli


试试看,


hi try this,


DataList dl = new DataList();
dl.DataSource = //Add Datasource
dl.DataBind();
this.form1.Controls.Add(dl);


这篇关于如何动态创建数据列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆