简体   繁体   中英

Get RepeaterItem in c# if code in a asp.net page <% … %>

Simple question and no answer somehwere ... What's wrong?

ASP.NET example:

<asp:Repeater ID="myRepeater" runat="server">
    <ItemTemplate>
        <% if(!string.IsNullOrWhiteSpace(((Message)((RepeaterItem)Eval("Container")).DataItem).text)) <%-- <- Exception --%>
           { %>
        <div class="msg">
            <%# ((Message)Container.DataItem).text %> <%-- <- This works fine! --%>
        </div>
        <% } %>
    </ItemTemplate>
</asp:Repeater>

Exception in line 3: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

I don't understand why this is not working >.<

Thanks in Advance for ideas or informations.

you can use this code

<asp:Repeater ID="myRepeater" runat="server">
    <ItemTemplate>
        <div runat="server" visible='<%# !string.IsNullOrWhiteSpace(((Message)((RepeaterItem)Eval("Container")).DataItem).text)%>'
            <div class="msg">
                <%# ((Message)Container.DataItem).text %>
            </div>
        </div>
</ItemTemplate>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM