accessing user web control in code behind

Posted on 2008-11-16
Last Modified: 2012-06-27

i have a user control for an add to cart button i created that i need to add to a number of pages. the problem i have is that i have included the control on the page i.e.

<cart:add_to_cart id="cart_button" runat="server" />

however i cannot access the cart_button in the code behind inthe page_load method.

can anyone tell me if this is possible and if so how to do it?
Question by:flynny
    LVL 20

    Expert Comment

    Hi there,

    Have a look at this link on how to access UserControls from your page code-behind:


    Author Comment

    hi thanks for the reply.

    thanks for the link. this is what i'm doing though i think. in the aspx file i register the control


    <%@ Register TagPrefix="cart" TagName="add_to_cart" Src="~/cart/add_to_cart_control/add_to_cart.ascx" %>

    <cart:add_to_cart id="cart_button" runat="server" />


    Protected addButton As add_to_cart

    here i get the error 'type 'add_to_cart' is not defined'

    now from looking around the web, i have seen suggestion that i will have to make my usercontrol a dll in order to access it? is this the only way?
    LVL 20

    Accepted Solution


    To access a control like that, you need to register it differently. So, in markup, replace your declaration with:

    <%@ Reference Control="~/cart/add_to_cart_control/add_to_cart.ascx" %>

    Then, to add the control to the page, in code-behind page load:

    Dim cartControl As YourControlClass = CType(LoadControl"~/cart/add_to_cart_control/add_to_cart.ascx"), YourControlClass)

    (note that "YourControlClass" should be replaced by the actual control class (the name that appears at the top of the code-behind as "Partial Class....."

    Then, you can reference anything on the control, just type cartControl. and Intellisense is going to show you.

    Converting an UserControl to a .dll (CustomControl) is another option. You need to balance it's pros and cons. Have a look here to start:


    Author Comment

    great thanks for your help.

    that worked perfectly, i then used a placeholder to place the control in the right place.

    Out of interest, what is your opinion on the situation. The reason i have created the control is so i can manage how items are added to the cart quite easily. the button will be added to certain pages throughout the site including a search datagrid. would you go for the dll approach or the mehod i'm using?

    many thanks for the help,

    LVL 20

    Expert Comment

    As a personal preference, I always tend to go for the custom control approach, unless the control I'm creating is mostly html markup with almost no logic.
    In the end, any way you take, you are creating an object,  and it could be used as such, with the little differences that the link I posted explains.
    In your situation, if you don't plan the redistribute the control, or make it part of many different applications, just stay with the UserControl. After all, converting it to a CC at a later stage is not so difficult.

    Author Closing Comment

    quickly and helpfullly guided through to the solution. many thanks

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This video discusses moving either the default database or any database to a new volume.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now