Repeater控件绑定的三种方式
方式一
在aspx页面,写好需要循环输出的内容,一般包含用户自定义控件、服务器控件、html格式的片段、和<%# eval("name")%>这种方式来动态显示获取到得数据列表:
<asp:repeater id="rpimage" runat="server">
<itemtemplate>
<li>
<a href="//www.jb51.net/lmfeng/archive/2012/03/06/<%# (container.dataitem as productimage).resourceurl%>" class="<%# container.itemindex == 0 ? "currimg " : "" %>">
<img src="//www.jb51.net/lmfeng/archive/2012/03/06/<%# (container.dataitem as productimage).resourceurl%>"
class="<%# (container.dataitem as productimage).imageversion)%>">
<%# eval("name").tostring() %>
</a>
</li>
</itemtemplate>
</asp:repeater>
在cs文件,是用getproductimagelist方法来获取list<productimage>类型的数据列表,并绑定在repeater控件上面:
上面的不包含用户自定义控件、服务器控件,所以不需要itemdatabound事件来对单个的数据项进行个性化的赋值
protected override void binddatasource()
{
this.rpimage.datasource = getproductimagelist();
this.rpimage.databind();
}
方式二
在aspx页面,这次包含了用户自定义控件,所以需要用到itemdatabound事件来对列表中的每一个用户自定义控件进行个性化的赋值,用户自定义控件可以有公用的方法或者属性,
让我们在itemdatabound事件中赋值:
<asp:repeater id="gvitemlist" runat="server" enableviewstate="false">
<itemtemplate>
<li>
<uccommon:imagecell id="imagecell" runat="server" />
<a href="//www.jb51.net/lmfeng/archive/2012/03/06/###" title='<%# eval("name").tostring() %>' href='//www.jb51.net/lmfeng/archive/2012/03/06/<%# eval("code").tostring()%>'>
<uccommon:productfullnamecell id="productfullnamecell" runat="server" />
</a>
<uccommon:ucproductcontrolcell id="productcontrolcell" runat="server"/>
</li>
</itemtemplate>
</asp:repeater>
在cs文件,用户自定义控件可以有公用的方法或者属性,让我们在itemdatabound事件中赋值:
protected override void binddatasource()
{
this.gvitemlist.datasource = productlist;
this.gvitemlist.databind();
}
protected override void oninit(eventargs e)
{
this.gvitemlist.itemdatabound += new repeateritemeventhandler(this.onitemlistdatabound);
base.oninit(e);
}
private void onitemlistdatabound(object sender, repeateritemeventargs e)
{
productcellinfo productitem = (productcellinfo)e.item.dataitem;
if (productitem != null)
{
productfullnamecell productname;
imagecell image;
productcontrolcell productcontrolcell;
foreach (control sub in e.item.controls)
{
productname = sub as productfullnamecell;
if (productname != null)
{
productname.initproductfullname(productitem.title, productitem.promotiontitle, dispalycontentlength);
continue;
}
image = sub as imagecell;
if (image != null)
{
image.initimagecell2(productitem.id, productitem.code, productitem.name, productitem.imageurl, productitem.imageversion);
continue;
}
productcontrolcell = sub as productcontrolcell;
if (productcontrolcell != null)
{
productcontrolcell.initproductcontrolcell(productitem);
continue;
}
}
}
}
方式三:
在aspx页面,可以显示设置onitemdatabound属性,就不用像方式二那样,在cs文件中的oninit方法中动态绑定,代码如下:
<asp:repeater id="rptlistcell" runat="server" onitemdatabound="rptallonitemdatabound">
<itemtemplate>
<li>
<a href='//www.jb51.net/lmfeng/archive/2012/03/06/<%#eval("id"))>' title='<%#encode(eval("name")) %>'>
<span><%#encode(eval("name")) %></span>
<asp:placeholder id="pnew" runat="server" visible="false"></asp:placeholder>
<asp:placeholder id="phot" runat="server" visible="false"></asp:placeholder>
<asp:literal id="literalvalidgiftoption" runat="server"></asp:literal>
</a>
</li>
</itemtemplate>
</asp:repeater>
在cs文件:
protected override void binddatasource()
{
base.binddatasource();
this.rptlistcell.datasource = this.list;
this.rptlistcell.databind();
}
protected void rptallonitemdatabound(object sender, repeateritemeventargs e)
{
categoryinfo category = (categoryinfo)e.item.dataitem;
placeholder phot = e.item.findcontrol("phot") as placeholder;
placeholder pnew = e.item.findcontrol("pnew") as placeholder;
literal lit = e.item.findcontrol("literalvalidgiftoption") as literal;
switch (category.promotionstatus)
{
case "h":
phot.visible = true;
break;
case "n":
pnew.visible = true;
break;
default:
break;
}
lit.text = category.name;
}