Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store value of data binding expression into variable

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.

like image 499
user46703 Avatar asked Jul 13 '09 21:07

user46703


People also ask

Which data binding allows you to take a variable a property or an expression and insert it dynamically into a page?

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.

Which method is used to express single-value data binding on Web page?

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).

Which method activates the binding in repeated value data binding?

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.


2 Answers

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 :-)

like image 75
Robert Avatar answered Sep 18 '22 18:09

Robert


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

like image 33
iburlakov Avatar answered Sep 19 '22 18:09

iburlakov