Note that there are some explanatory texts on larger screens.

plurals
  1. POFindControl method cannot locate the control on the page
    primarykey
    data
    text
    <p>Ive tried searching for hours now and cannot find out why my code (aka, me.) is failing</p> <p>Basically... I have a listview control which I'm passing a datatable of products (ID, Name, Description and Price columns), and im trying to make it so that when the "checkout" button is pressed, it parses through all the controls on the page, finds all the controls with the correct ID's and adds the items values to the cart.</p> <p>ive checked all my ID's in the source code and they match up to the ones being requested by the FindControl method.</p> <p>the error getting thrown back is:</p> <pre><code>Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Line 21: For I = 1 To counter Line 22: Dim cartItem As New Core.Types.CartItem Line 23: cartItem.Name = CType(productsContainer.FindControl("product" + I.ToString()), HtmlGenericControl).InnerText Line 24: cartItem.Quantity = Convert.ToInt32(CType(productsContainer.FindControl("quantity" + I.ToString()), HtmlSelect).Value) Line 25: cartItem.Price = Convert.ToDecimal(CType(productsContainer.FindControl("price" + I.ToString()), HtmlGenericControl).InnerText.Remove(0, 1)) </code></pre> <p>my .aspx code:</p> <pre><code> &lt;div class="productsContainer" id="productsContainer" runat="server"&gt; &lt;asp:ListView runat="server" ID="lsvProducts"&gt; &lt;LayoutTemplate&gt; &lt;ul class="lsvProducts"&gt; &lt;li class="highlight"&gt; &lt;div class="productName"&gt; Product &lt;/div&gt; &lt;div class="productQuantity"&gt; Number of Licenses &lt;/div&gt; &lt;div class="productPrice"&gt; Price &lt;/div&gt; &lt;/li&gt; &lt;asp:PlaceHolder ID="itemPlaceHolder" runat="server"&gt;&lt;/asp:PlaceHolder&gt; &lt;/ul&gt; &lt;/LayoutTemplate&gt; &lt;ItemTemplate&gt; &lt;li&gt; &lt;div style="display: none;"&gt; &lt;%=setCurrent()%&gt; &lt;/div&gt; &lt;input type="hidden" id='productID&lt;%#Eval("ID")%&gt;' /&gt; &lt;div class="productName" id='product&lt;%=currentItem%&gt;'&gt; &lt;%#Eval("Name")%&gt; &lt;/div&gt; &lt;div class="productQuantity"&gt; &lt;select id='quantity&lt;%=currentItem%&gt;'&gt; &lt;option selected="selected" value="0"&gt;0&lt;/option&gt; &lt;option value="1"&gt;1&lt;/option&gt; &lt;option value="2"&gt;2&lt;/option&gt; &lt;option value="3"&gt;3&lt;/option&gt; &lt;option value="4"&gt;4&lt;/option&gt; &lt;option value="5"&gt;5&lt;/option&gt; &lt;option value="6"&gt;6&lt;/option&gt; &lt;option value="7"&gt;7&lt;/option&gt; &lt;option value="8"&gt;8&lt;/option&gt; &lt;option value="9"&gt;9&lt;/option&gt; &lt;option value="10"&gt;10&lt;/option&gt; &lt;/select&gt; &lt;/div&gt; &lt;div class="productPrice" id='price&lt;%=currentItem%&gt;'&gt; &lt;%#"$" + Convert.ToDouble(Eval("Price")).ToString()%&gt; &lt;/div&gt; &lt;/li&gt; &lt;/ItemTemplate&gt; &lt;/asp:ListView&gt; &lt;/div&gt; &lt;div class="clearer"&gt; &amp;nbsp;&lt;/div&gt; &lt;div class="purchaseButton"&gt; &lt;asp:Button ID="btnAddCart" runat="server" Text="Add to Cart" /&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>and my code behind:</p> <pre><code> Dim counter As Int32 Public currentItem As Int32 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 'get all active products to display in the listing Dim query As String = "SELECT * FROM Products WHERE Active = 1" Dim dt As DataTable = DAL.Data.GetDataTable(query, "MainDB") counter = dt.Rows.Count lsvProducts.DataSource = dt lsvProducts.DataBind() End Sub Protected Sub btnAddCart_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAddCart.Click 'create a new instance of the cart Dim cart As New Core.Types.Cart 'foreach item in the listing, find its details and add it to the shopping cart For I = 1 To counter Dim cartItem As New Core.Types.CartItem cartItem.Name = CType(productsContainer.FindControl("product" + I.ToString()), HtmlGenericControl).InnerText cartItem.Quantity = Convert.ToInt32(CType(productsContainer.FindControl("quantity" + I.ToString()), HtmlSelect).Value) cartItem.Price = Convert.ToDecimal(CType(productsContainer.FindControl("price" + I.ToString()), HtmlGenericControl).InnerText.Remove(0, 1)) cartItem.ID = Convert.ToInt32(CType(productsContainer.FindControl("productID" + I.ToString()), HtmlGenericControl).InnerText) cart.AddItem(cartItem) Next If (cart.isEmpty) Then 'empty cart, go nowhere. show a message saying the carts empty and to choose something. Else Response.Redirect("~/Checkout.aspx") End If End Sub Public Function setCurrent() currentItem = currentItem + 1 Return currentItem End Function </code></pre> <p>Please help... this is driving me insane!</p> <p>Thanks in advance :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload