I have a website project in VS2010 and, in short, I am using one control “BasketMenuItem.ascx” to display the cart details, in summary at the top of the page. What
In the code to render the navigation, I have:
<tp:BasketMenuItem ID="basketMenuItem" runat="server" />
I then have another control, on every product page with a button that adds X number of Product Items to the cart. On adding the items, I use javascript to highlight the cart, i then want to refresh the “BasketMenuItem” control so that the correct details are displayed.
I thought i could do something like (“usercontrols_Global_BasketMenuItem” is the class of the BasketMenuItem control):
(usercontrols_Global_BasketMenuItem)Page.FindControl("basketMenuItem").LoadItems();
However, i get an error saying:
"The Type or Namespace Name BasketMenuItem could not be found (are you missing a using directive or an assembly reference?)"
Im unsure how to get around this. Im not sure what i would need to include, as i cannot simple put:
using usercontrols_Global_BasketMenuItem;
I’ve also tried adding a ClassName attribute to the control file like so:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="BasketMenuItem.ascx.cs" ClassName="BasketMenuItem" Inherits="usercontrols_Global_BasketMenuItem" %>
Yet i still cannot manage to reference it.
The Code
First of all, there is the BasketMenuItem control. Both ascx and ascx.cs. This contains the methods to load the contents of the current basket. “LoadItems()”.
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="BasketMenuItem.ascx.cs" ClassName="BasketMenuItem" Inherits="BasketMenuItem" %>
public partial class BasketMenuItem : System.Web.UI.UserControl
{
//...
}
Then, there is the Menu control. This is where the Basket control is placed on the page in the navigation:
<ul>
<li>...</li>
<li>...</li>
<li>...</li>
<!-- Cart -->
<tp:BasketMenuItem ID="basketMenuItem" runat="server" />
</ul>
Finally, there is the ListItems Control, defined as follows. This control contains the button to add items into the cart. Here I want to find the BasketMenuItem control on the page and call its “LoadItems()” methods to update.
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ListItems.ascx.cs" Inherits="ListItems" %>
public partial class ListItems : System.Web.UI.UserControl
{
//...
}
No namespaces have been specified, from what I have read I would assume I could find the BasketItem control in the ASP namespace. But i cannot.
You are on the right track with the using statement. But instead of doing a using statement with the classname, you need to use the namespace of the control.
If you open up the codebehind (.cs) of your usercontrols_Global_BasketMenuItem control you should be able to find the namespace you need to include by looking for the namespace clause in the code file.
Also, while in that file you should verify whether the class name is ‘usercontrols_Global_BasketMenuItem’ or is ‘BasketMenuItem’. The latter seems more likely.
If you do not have namespaces, you can try using the “global::” namespace and accessing it that way, or aliasing it in a using statement. See this post for details: explicitly refer to a class without a namespace in C#
However, I strongly suggest updating to add namespaces. Architecturally you will find it much easier to work with your codebase and have similar class names if you have proper namespace structures.