views:

688

answers:

1

In my ListView I want to use a property of the Container in an if statement on the aspx page as shown below. But I'm getting a "The name 'Container' does not exist in the current context" error. Can I not the Container in an if statement?

   <ItemTemplate>
        <tr>
          <td>
            <% if (EDIT_INDEX == (((ListViewItem)Container) as ListViewDataItem).DataItemIndex )
               {%>
            <span id="row<%#(((ListViewItem)Container) as ListViewDataItem).DataItemIndex %>"
Some Stuff
       </span>
<% } %>
+5  A: 

Container is only available in binding expressions. Use a <%# .. %> block with the ternary operator (?:) and string concatenation to achieve the same thing.

Another solution that I have used is to put stuff in different <asp:Placeholder> controls whose Visible properties are binded to different boolean expressions and put the different possible representations inside those placeholders. Something like:

<ItemTemplate>
    <tr>
      <td>
   <asp:Placeholder runat="server" 
    Visible='<%# EDIT_INDEX == (((ListViewItem)Container) as ListViewDataItem).DataItemIndex %>'>
        <span id='row<%#(((ListViewItem)Container) as ListViewDataItem).DataItemIndex %>'>
            Some Stuff
        </span>
   </asp:Placeholder>
Mehrdad Afshari
I do agree with the same...
Meetu Choudhary