accessing user web control in code behind

hi,

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?
flynnyAsked:
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.

Juan_BarreraCommented:
Hi there,

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

http://www.c-sharpcorner.com/UploadFile/Santhi.M/PassingValuesfrmUCtoASPX11212005050040AM/PassingValuesfrmUCtoASPX.aspx

0
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

----------------------------------------------aspx------------------------------------------------------------------

<%@ 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" />


----------------------------------------------aspx.vb--------------------------------------------------------------

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?
0
Juan_BarreraCommented:
flynny,

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)
PagePlaceholder.Controls.Add(cartControl)

(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:

http://support.microsoft.com/kb/893667

0

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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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,

matt.
0
Juan_BarreraCommented:
Matt,
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.
0
flynnyAuthor Commented:
quickly and helpfullly guided through to the solution. many thanks
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.