直放站的ItemTemplate服务器端 [英] Repeater ItemTemplate Server Side

查看:102
本文介绍了直放站的ItemTemplate服务器端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临的一个问题,访问HTML元素。 。在服务器端这些元素存在于的ItemTemplate A Repeater控件

I am facing a problem with access to HTML elements. On the server side those elements exists in itemTemplate of a repeater control.

简单的代码:

    <asp:Repeater ID="rptList" runat="server" onitemcreated="rptList_ItemCreated">
    <HeaderTemplate>
               <div id="accordionGiftLelo">
    </HeaderTemplate>
    <ItemTemplate>


                         <div class="set">
                         <div class="title"><img src='<%# Eval("TitleImgUrl")%>'  runat="server" width="198" height="29"  /></div>
                         <div ID="RepeaterBG" class="content"  style="background-color:<%# Eval("ContentBackground")%>">
                             <%--<a href='<%# Eval("ContentUrl")%>'><img src='<%# Eval("ContentImgUrl")%>' alt="Embroidered Devil Eyes" width="198" height="198" border="0" /></a>--%>
                             <div style="width:198"><asp:Literal ID="Literal1" runat="server"  Text='<%# Eval("ContentImgUrl")%>'></asp:Literal></div>
                         </div>
                         </div>

    </ItemTemplate>

    <FooterTemplate>
                         </div>
    </FooterTemplate>

    </asp:Repeater>



如何访问例如 RepeaterBG DIV ?

推荐答案

下面是关于如何访问控制服务器端和数据适用于他们的例子。请务必添加=服务器div标签。

Here is an example on how to access controls server side and apply your data to them. Make sure to add the RunAt="server" to the div tag.

添加为ItemDataBound事件

add a reference for the ItemDataBound event

rptList.ItemDataBound += rptList_ItemDataBound;

private void rptList_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{   
    if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return;

    HtmlControls.HtmlGenericControl RepeaterBG = (HtmlControls.HtmlGenericControl)e.Item.FindControl("RepeaterBG");

    Data.DataRowView dr = (Data.DataRowView)e.Item.DataItem;

    RepeaterBG.Style.Add("background-color", dr("ContentBackground"))
}

您会发现,如果你这样做的所有控件,而不是使用<%#评估和演示,你的代码会更易于维护。

You will find that if you do this for ALL of your controls, instead of using "<%# Eval", you're code will be much easier to maintain.

这篇关于直放站的ItemTemplate服务器端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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