?
Solved

accessing user web control in code behind

Posted on 2008-11-16
6
Medium Priority
?
320 Views
Last Modified: 2012-06-27
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?
0
Comment
Question by:flynny
  • 3
  • 3
6 Comments
 
LVL 20

Expert Comment

by:Juan_Barrera
ID: 22972625
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
 

Author Comment

by:flynny
ID: 22973984
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
 
LVL 20

Accepted Solution

by:
Juan_Barrera earned 2000 total points
ID: 22979183
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:flynny
ID: 22983147
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
 
LVL 20

Expert Comment

by:Juan_Barrera
ID: 22983473
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
 

Author Closing Comment

by:flynny
ID: 31517289
quickly and helpfullly guided through to the solution. many thanks
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Integration Management Part 2
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses
Course of the Month7 days, 21 hours left to enroll

615 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