Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6059189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:40:57+00:00 2026-05-23T08:40:57+00:00

below is my listview code example, while the products are populating, I want to

  • 0

below is my listview code example, while the products are populating, I want to check every single one of them against my database whether it’s quantity is 0, how can add a method in code behind to do this:

I tried doing this under the onselectedindexchanged event but got the ‘Object reference not set to an instance of an object.’ exception :

Appreciate any help given. Thanks!

         using (XXXShop db = new XXXShop ())
        {
             var availability = from p in db.Products
                                where p.Quantity == 0
                                select p;

        (ListView_Products.FindControl("litStatus") as Literal).Text = "Unavailable";
        }         

ListView code:

    <asp:ListView ID="ListView_Products" runat="server" DataKeyNames="ProductID"  
    DataSourceID="EDS_ProductsByCategory" GroupItemCount="3" 
    onselectedindexchanged="Page_Load">
   <EmptyDataTemplate><table runat="server"><tr><td>No data was returned.</td></tr></table></EmptyDataTemplate>
    <EmptyItemTemplate><td runat="server" /></EmptyItemTemplate>
    <GroupTemplate><tr ID="itemPlaceholderContainer" runat="server"><td ID="itemPlaceholder" runat="server"></td></tr></GroupTemplate>
    <ItemTemplate>
        <td runat="server">
            <table border="0" width="295">
              <%-- Product pics --%>
                <tr>        
                    <td style="vertical-align: middle; text-align: center;">
                        <a href='ProductDetails.aspx?productID=<%# Eval("ProductID") %>'>
                            <image src='Catalog/Images/Thumbs/<%# Eval("ProductImage") %>' width="195" height="270" border="0">
                        </a>
                    </td>
                </tr>
                <tr>
                    <%-- Product info, description, price...etc --%>
                     <td style="width: 250px; vertical-align: middle; text-align: center; padding-bottom: 5px;">                          
                     <b>Price: </b><%# Eval("UnitCost", "{0:c}")%>                 
                    </td>                                       
                </tr>
                <tr>
                     <td style="width: 250px; vertical-align: middle; text-align: center; padding-bottom: 25px;">
                         <asp:Literal ID="litStatus" runat="server" />
                     </td>
                </tr>                               
              </table>

        </td>
    </ItemTemplate>
    <LayoutTemplate>
        <table runat="server">
            <tr runat="server">
                <td runat="server">
                    <table ID="groupPlaceholderContainer" runat="server">
                        <tr ID="groupPlaceholder" runat="server"></tr>
                    </table>
                </td>
            </tr>
            <tr runat="server"><td runat="server"></td></tr>
        </table>
    </LayoutTemplate>
</asp:ListView>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:40:58+00:00Added an answer on May 23, 2026 at 8:40 am

    I haven’t done that much with ASP.NET or used the ListView so i don’t know if there might be better ways to do this, but you could add a method to your codebehind that can format your data the way you want. Like the following:

    protected string FormattedQuantity(int quantity)
    {
      return quantity > 0 ? quantity.ToString() : "Unavailable";
    }
    

    You should then be able to use this formatter anywhere in your aspx file by doing the following:

    <asp:Label ID="OccurrenceLabel" runat="server" 
      Text='<%# FormattedQuantity((int)Eval("Quantity")) %>' />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use the below code to create listview without listactivity.I need to know how
The code below lets me show emails received in a listview on when the
using the below code, I have inserted the data in the listview var renderItemElement
I have a Listview and a button like this Below is the code I
Below I have a very simple example of what I'm trying to do. I
Below is the code of a simple html with a table layout. In FF
Below is a stored procedure to check if there is a duplicate entry in
I need help explaing the following example below (c) CommonsWare. I know it makes
I have a listview, where I set the datasource in the code-behind - this
I have a ListView with a CheckBox as one of the columns, bound to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.