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.
Single-Value Data Binding (simple): Allows you to take a Page variable, property, or an expression and insert it dynamically into a page. In other words, it allows you to pop data into HTML elements.
16.2. Single-Value Data Binding. When you call the DataBind() method for the page, this text will be replaced with the value for Country (for example, Spain).
To use repeated-value binding, you link one of these controls to a data source (such as a field in a data table). When you call DataBind(), the control automatically creates a full list using all the corresponding values.
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 :-)
You can use OnItemDataBount
event and work with DataItem as with variable there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With