User Controls

Last post 04-07-2008 10:41 AM by Khai Wan. 9 replies.

Sort Posts:

  • User Controls

    12-21-2007, 5:50 AM
    • Member
      98 point Member
    • random0xff
    • Member since 02-11-2007, 2:34 PM
    • Posts 208

    I have a simple user control to display a 'Product' and it needs the id of the product and the name of the product. I gave the control two public properties for that. If I put the user control in a Repeater I can use databinding syntax ProductName='<%# Eval("Name") %>'. If I write a foreach loop then inside the loop I can have a strongly typed reference to each Product. Is it possible to use the User Control inside a foreach loop and set the properties using the strongly typed object? I tried ProductName="<%= product.Name %>" but its not set to anything.

  • Re: User Controls

    12-21-2007, 7:31 AM

    HI

     Try this

       ProductName="<%= Name %>"
     

    HI

    Happy Smiling

    Regards
    Karthikeyan
    http://www.karthidotnet.blogspot.com/
  • Re: User Controls

    12-21-2007, 8:49 AM
    • Member
      98 point Member
    • random0xff
    • Member since 02-11-2007, 2:34 PM
    • Posts 208

    That doesn't work, ProductName will still be emtpy.

  • Re: User Controls

    12-21-2007, 9:29 AM
    • All-Star
      21,648 point All-Star
    • gunteman
    • Member since 07-11-2007, 12:57 PM
    • Norrköping, Sweden
    • Posts 3,177
    Could you please show us some more code, so that we'll understand the context.
    -- "Mark As Answer" if my reply helped you --
  • Re: User Controls

    12-21-2007, 9:40 AM
    • Member
      98 point Member
    • random0xff
    • Member since 02-11-2007, 2:34 PM
    • Posts 208
     
    <% foreach(Product p in ViewData.Products) { %> 
    
    <uc:ProductDisplay ID="ProductDisplay1" runat="server" ProductName="<%= Product.Name %>" />
    
    <% } %> 
     

    How do you get anything from ViewData into the ProductName attribute? The above doesn't work, and neither does <%# product.Name %> ("The name 'product' does not exist in the current context")

  • Re: User Controls

    12-21-2007, 9:41 AM
    Answer

    Hi, yes, if you don't want to use the DataBinding method, you can use MVCToolkit's RenderUserControl method.  The code would be like this:

     

    <% foreach(Product prod in ViewData.ProductCollection) { %>
    <%= Html.RenderUserControl("~/Path/ProductControl.ascx", null, new { ProductID = prod.ProductID, ProductName = prod.ProductName })%>
    <% } %>
      

    The second parameter to RenderUserControl lets you supply ViewData, and the third lets you initialize its public properties.


  • Re: User Controls

    12-21-2007, 9:45 AM
    • Contributor
      7,054 point Contributor
    • rjcox
    • Member since 12-19-2007, 2:14 PM
    • Basingstoke, UK
    • Posts 1,444

    Have you tried  

    <%= p.Name %>
     ?

    (Product.Name is a static field/property on the Product type). 

    Richard
  • Re: User Controls

    12-21-2007, 10:56 AM
    Answer
    • Member
      506 point Member
    • JeremyS
    • Member since 10-21-2006, 8:23 AM
    • UK
    • Posts 99
    random0xff:
    How do you get anything from ViewData into the ProductName attribute? The above doesn't work, and neither does <%# product.Name %> ("The name 'product' does not exist in the current context")

    You could use the CodeExpressionBuilder found here to do this. This way you could write:

    <uc:ProductDisplay ID="ProductDisplay1" runat="server" ProductName="<%$ code: p.Name %>" />
    

    This is the approach that I've been using with my existing server controls.

  • Re: User Controls

    12-25-2007, 1:40 AM
    • Participant
      852 point Participant
    • robconery
    • Member since 02-23-2005, 10:16 PM
    • Posts 195
    • AspNetTeam

     The problem here is that your code is using "Product.Name" which is not defined - your loop is using the instance "p" so you need "p.ProductName" - that's how you'd do it with your code.

    You can also do this in the UserControl itself if you use the MVC Toolkit:

    <%=Html.RenderUserControl("MyProductDisplay.ascx")%>

    If you declare "MyProductDisplay.ascx" as a ViewUserControl<Product> (assuming your view is a ViewPage<Product>) - they will "auto share" the ViewData so that in the user control you can access ViewData.

    You can also explicitly set the data on a UserControl:

    <%=Html.RenderUserControl("MyProductDisplay",ViewData.Product)%>

    This is a nice option if you don't want to share ViewData.

    It's Christmas Eve and Santa is on the roof...
     

  • Re: User Controls

    04-07-2008, 10:41 AM
    • Member
      2 point Member
    • Khai Wan
    • Member since 04-07-2008, 10:39 AM
    • Posts 1

    Thanks for this tips. It is very helpful.  

Page 1 of 1 (10 items)