AJM
AJM

Reputation: 32490

ASP.NET Using If statement with Container in a list view

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>
<% } %>

Upvotes: 1

Views: 3500

Answers (1)

Mehrdad Afshari
Mehrdad Afshari

Reputation: 421998

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>

Upvotes: 8

Related Questions