views:

298

answers:

2

Hi,

I need to access the value of a bound item several times in a template. Right now my ListView template looks like this:

<asp:ListView ID="ListView1" runat="server" ItemPlaceholderID="plc"><br/>
 <ItemTemplate><br/>
  <input type="radio" class="myrating<%# DataBinder.Eval(Container.DataItem, "Day")%>" value="3" /><br/>
  <input type="radio" class="myrating<%# DataBinder.Eval(Container.DataItem, "Day")%>" value="4" /><br/>
    </ItemTemplate><br/>
    <LayoutTemplate><br/>
     <div id="plc" runat="server"><br/>
     </div><br/>
    </LayoutTemplate><br/>
    <EmptyDataTemplate><br/>
     No data</EmptyDataTemplate><br/>
</asp:ListView><br/>

Under certain conditions I may have dozens of radio button so repeatedly calling <%# DataBinder.Eval(Container.DataItem, "Day")%> seems to be inefficient.

I would like to assign the value of that expression to a variable and then use this variable instead so my template would look something like this

<ItemTemplate><br />
<%String ClassName = "myrating" + <%# DataBinder.Eval(Container.DataItem, "Day")%><br />
  <input type="radio" class="<%=ClassName %>" value="3" /><br />
  <input type="radio" class="<%="ClassName" value="4" /><br />
    </ItemTemplate><br />

This example doesn't compile but I hope you are getting the idea.

+2  A: 

You can use OnItemDataBount event and work with DataItem as with variable there.

iburlakov
A: 

You can give your page a public variable MyRating.

Now you can assign the variable in the expression binding Syntax:

   <ItemTemplate>
         <%# MyRating = "myrating" + <%# Eval(Container.DataItem, "Day")%>
         //Use the variable inside the binding(!) block 
         <%#MyRating
   </ItemTemplate>

I usually bind to lists of view-objects. That way I can access view properties directly.

   <ItemTemplate>
         <%# MyType = (MyType)Container.DataItem 
         <%# MyRating.Average %> 
         <%# MyRating.Count %> 
   </ItemTemplate>

Hope this helps :-)

Robert