实现System.Web.UI.ITemplate接口来动态设置控件模板

代码其实比较简单了,相信大家能够看得懂,我就不多作解释了。

None.gif public   class  MyTemplate : ITemplate
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ContractedSubBlock.gifExpandedSubBlockStart.gif    
ITemplate 成员#region ITemplate 成员
InBlock.gif
InBlock.gif    
public void InstantiateIn(Control container)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Literal lbl 
= new Literal();
InBlock.gif        lbl.DataBinding 
+= new EventHandler(lbl_DataBinding);//关键在这里
InBlock.gif
        container.Controls.Add(lbl);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif
InBlock.gif    
private void lbl_DataBinding(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Literal lbl 
= (Literal)sender;
InBlock.gif        
if (DataBinder.Eval(((DataListItem)lbl.NamingContainer).DataItem, "HaveImg").ToString() == "False")
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            lbl.Text 
= "<a href=\"" + DataBinder.Eval(((DataListItem)lbl.NamingContainer).DataItem, "Link").ToString() + "\">" + DataBinder.Eval(((DataListItem)lbl.NamingContainer).DataItem, "Name").ToString() + "</a>";
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
InBlock.gif            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>";
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

用的时候
None.gif datalist.ItemTemplate  =   new  MyTemplate();

转载于:https://www.cnblogs.com/DavidFan/archive/2007/06/01/767853.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值