代码其实比较简单了,相信大家能够看得懂,我就不多作解释了。
public class MyTemplate : ITemplate
{
ITemplate 成员#region ITemplate 成员
public void InstantiateIn(Control container)
{
Literal lbl = new Literal();
lbl.DataBinding += new EventHandler(lbl_DataBinding);//关键在这里
container.Controls.Add(lbl);
}
#endregion
private void lbl_DataBinding(object sender, EventArgs e)
{
Literal lbl = (Literal)sender;
if (DataBinder.Eval(((DataListItem)lbl.NamingContainer).DataItem, "HaveImg").ToString() == "False")
{
lbl.Text = "<a href=\"" + DataBinder.Eval(((DataListItem)lbl.NamingContainer).DataItem, "Link").ToString() + "\">" + DataBinder.Eval(((DataListItem)lbl.NamingContainer).DataItem, "Name").ToString() + "</a>";
}
else
lbl.Text = "<a href=\"" + DataBinder.Eval(((DataListItem)lbl.NamingContainer).DataItem, "Link").ToString() + "\"><img alt=\"" + DataBinder.Eval(((DataListItem)lbl.NamingContainer).DataItem, "Name").ToString() + "\" src=\"" + DataBinder.Eval(((DataListItem)lbl.NamingContainer).DataItem, "ImgUrl").ToString() + "\" border=\"0\" width=\"95\" height=\"38\" /></a>";
}
}
用的时候
datalist.ItemTemplate = new MyTemplate(); |