简体   繁体   中英

How to use Eval() data in a method within a repeater

I've written a method that checks and returns the status code of a URL. Then I used a repeater to grab rows from my db. What I'm trying to figure out is how to use the data returned in the repeater in a method called within the repeater. Here's the code for the ItemTemplate.

<ItemTemplate>
    <tr class="alt">
        <td><%# Eval("Bounce_from") %></td>
        <td><%# Eval("Bounce_to") %></td>
        <td><%# Eval("is301") %></td>
        <td><% Response.Write(exist(Eval("Bounce_to")));  %></td>
    </tr>
</ItemTemplate>

<% Response.Write( exist(Eval("Bounce_to")) ); %> is where I'm struggling. I've searched and can't find the answer. Hopefully someone can point me in the right direction...thanks in advance.

Just as simple as

<td><%# exist(Eval("Bounce_to")) %></td>

This is assuming exist is available on the markup (it should at least protected page method) and accepts object as an input argument since Eval returns object.

Basically <%# %> runs whatever code is inside it in data binding context (thus Eval is available) and outputs the final value to the markup.

Without actually seeing your Repeater in entirety, it will be difficult to troubleshoot. But a working example, that may help you solve your issue.

<asp:Repeater Id="rpCustomer" runat="server" DataSourceId="dsCustomer">
     <ItemTemplate>

     </ItemTemplate>
</asp:Repeater>

The important part of the repeater, is the DataSourceId . You want to ensure it is correctly configured and indeed pulling the data out of the database.

The second part, is almost as if your attempting to call a method for the Eval() . That way you can modify the data, at any given point as the data source is iterating.

<!-- An Example -->
<%# Truncate(Eval("Description").ToString()) %>

So, in my case Truncate would be on the code behind like a traditional method:

protected string Truncate(string source)
{
     // Logic and Include valid Data Return
}

This will allow quick manipulation of data being pulled out of the database before it is displayed within the View State .

<!-- Final Result: -->
    <asp:Repeater Id="rpCustomer" runat="server" DataSourceId="dsCustomer">
         <ItemTemplate>
               <%# Truncate(Eval("Description").ToString()) %>
         </ItemTemplate>
    </asp:Repeater>

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