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>
I haven’t done that much with ASP.NET or used the
ListViewso 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:You should then be able to use this formatter anywhere in your aspx file by doing the following: