accessing user web control in code behind


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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi there,

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

flynnyAuthor Commented:
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?

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:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

flynnyAuthor Commented:
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,

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.
flynnyAuthor Commented:
quickly and helpfullly guided through to the solution. many thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.