title: Pass an object into a UserControl in a repeater? .Net 1
body: <p>I want to wrap up a chunk of HTML render logic in a User Control. Then I want to pass a custom product object (title, thumbnail, id, etc.) to the UserControl that it can use when it renders. Ideally I want to use this in a repeater (or for loop) and pass the current custom product object into the UC.</p>
  
  <p>Every example I've seen has been passing through strings on the UC tag but thats not really want I want to do as it means I'll have references everywhere that need updating should we add a new field that needs rendering.</p>
  
  <p>Any ideas?</p>
  
  <p>.Net 1 using VB.net (not my first choice for .net so go easy)</p>
  
  <p>HTML example to get us going, this would be in the .ascx page:</p>
  
  <pre><code>&lt;div&gt;
    &lt;h3&gt;&lt;%= myProd.title %&gt;&lt;/h3&gt;
    &lt;img src="&lt;%= myProd.thumbnail %&gt;" /&gt;
    &lt;p&gt;
      &lt;%= myProd.description %&gt;
    &lt;/p&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>UPDATE:</p>
  
  <p>Ok so with some hacking around here is what I've come up with, its not working though, whats the missing piece of this puzzle?</p>
  
  <p>Create a UserControl and add this to the code behind:</p>
  
  <pre><code>Public Class ProductRender
      Inherits System.Web.UI.UserControl
  
  #Region " Web Form Designer Generated Code "
  
      'This call is required by the Web Form Designer.
      &lt;System.Diagnostics.DebuggerStepThrough()&gt; Private Sub InitializeComponent()
  
      End Sub
  
      'NOTE: The following placeholder declaration is required by the Web Form Designer.
      'Do not delete or move it.
      Private designerPlaceholderDeclaration As System.Object
  
      Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
          'CODEGEN: This method call is required by the Web Form Designer
          'Do not modify it using the code editor.
          InitializeComponent()
      End Sub
  
  #End Region
  
      Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
          'Put user code to initialize the page here
      End Sub
  
      Private _product As productItem 
  
      Public Property myProd() As productItem 
          Get
              myProd = Me._product
          End Get
          Set(ByVal Value As productItem )
              Me._product = Value
          End Set
      End Property
  End Class
  </code></pre>
  
  <p>In the UC ascx page I simply have the html above.</p>
  
  <p>In the page that uses the UC I simply added the following for testing:</p>
  
  <pre><code>...usual ascx header stuff...
  &lt;%@ Register TagPrefix="ORC" TagName="productRender" Src="productRender.ascx" %&gt;
  &lt;ORC:productRender id="Assetrender1" runat="server" asset="&lt;%# getDummyProduct() %&gt;" /&gt;
  </code></pre>
  
  <p>in the code behind I declare getDummyProduct as so:</p>
  
  <pre><code>Public Function getDummyProduct() As productItem
      getDummyProduct = New productItem( "DVD Player", "It plays DVDs!", "some_thumb.jpg", 30 )
  End Function
  </code></pre>
  
  <p>Yet in my ascs page I get:"Object reference not set to an instance of an object"</p>
  
  <pre><code>Line 1:  &lt;%@ Control Language="vb" AutoEventWireup="false" Codebehind="ProductRender.ascx.vb" Inherits="MyApp.ProductRender" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %&gt;
  Line 2:  &lt;h3&gt;
  Line 3:  &lt;%= myProd.title %&gt;
  Line 4:  &lt;/h3&gt;
  </code></pre>