Link to home
Start Free TrialLog in
Avatar of flynny
flynnyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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?
Avatar of Juan_Barrera
Juan_Barrera
Flag of New Zealand image

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

Avatar of flynny

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of Juan_Barrera
Juan_Barrera
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of flynny

ASKER

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.
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.
Avatar of flynny

ASKER

quickly and helpfullly guided through to the solution. many thanks