本文介绍了如何动态创建数据列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码
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
推荐答案
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
DataList dl = new DataList();
dl.DataSource = //Add Datasource
dl.DataBind();
this.form1.Controls.Add(dl);
这篇关于如何动态创建数据列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!