Page 1 of 1

How to add a user control into a placeholder?

Posted: Thu Jul 30, 2009 11:54 am
by AbleMods
I've seen several cases in AC7 code where (what appears to be) an existing user control is added from the code-behind to a placeholder. Normally these are added as references on the HTML side.

For example:

Code: Select all

ASP.conlib_addtocartlink_ascx add2cart = new ASP.conlib_addtocartlink_ascx();
add2cart.ProductId = product.ProductId;
itemTemplate1.Controls.Add(add2cart);
How is that done? When I expose the ASP class, it's only listing very specific user controls. I'd like to add my own user control to a placeholder using the same technique.

Thoughts?

Re: How to add a user control into a placeholder?

Posted: Thu Jul 30, 2009 12:35 pm
by Logan Rhodehamel
In the aspx, add a registration entry. Look in the AC examples and look at the top of the file. You will see the user control is registered in the aspx. That makes it available in the code behind.

Be aware this only works in web site project model. Anyone who switches to the web application project model has to do something different.

Re: How to add a user control into a placeholder?

Posted: Thu Jul 30, 2009 12:50 pm
by AbleMods

Code: Select all

<%--
<conlib>
<summary>Displays a link to add an item to the cart.</summary>
<param name="ProductId">The ID of the product to add to the cart when the link is clicked.</param>
<param name="ShowImage" default="true">Indicates whether an image should be displayed with the link.  The image is determined by your theme.</param>
<param name="LinkText" default="">Text to use for the link.  You can use the string <b>{0}</b> as a place holder for the product name.</param>
</conlib>
--%>
This what you mean by "registration"?

Re: How to add a user control into a placeholder?

Posted: Thu Jul 30, 2009 3:22 pm
by Logan Rhodehamel

Code: Select all

<%@ Register Src="~/ConLib/OrderTotalSummary.ascx" TagName="OrderTotalSummary" TagPrefix="uc" %>
Like that.

Re: How to add a user control into a placeholder?

Posted: Thu Jul 30, 2009 3:45 pm
by nickc

Code: Select all

            Control control = this.LoadControl(virtualPathOfControlFile); // 
            myPlaceholder.Controls.Add(control);