Reputation: 8198
I have created a folder called "Controls" in my asp.net application. In that folder are user controls (1 being DateRangePicker.ascx for example). Now I am creating a class in my app code folder and I am trying to cast something to a DateRangePicker,
DateRangePicker drp = (CambitCMS.Controls.DateRangePicker)gvListing.Parent.Parent.FindControl("ucDateRangePicker");
And I am getting the following error: Error 2 The type or namespace name 'Controls' does not exist in the namespace 'CambitCMS' (are you missing an assembly reference?)
How do I properly reference this user control in a class?
I want my "ListingsBasePage" class to reference my DateRangePicker usercontrol, but if I put a namespace around the whole user control code it breaks the user control, it says all the things like the textboxes, labels, etc dont exist in current context when a namespace is wrapped around the code.
Here is the start of the class "ListingsBasePage" where I am triyng to reference the usercontrol
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class ListingsBasePage : System.Web.UI.Page
{
protected void gvListing_OnPreRender(object sender, EventArgs e)
{
protected void gvListing_OnPreRender(object sender, EventArgs e)
{
GridView gvListing = (GridView)sender;
DateRangePicker drp = (CambitCMS.Controls.DateRangePicker)gvListing.Parent.Parent.FindControl("ucDateRangePicker");
gvListing.PageSize = drp.recordsPerPage;
}
........
And the start of the DatRangePickerControl
using System;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Controls_DateRangePicker : System.Web.UI.UserControl
{
public String FromDate
{
get
{
return tbFromDate.Text;
}
}
Upvotes: 2
Views: 1464
Reputation: 15262
This comes up time and time again with website projects. There is no concept of namespaces in website projects and referencing controls can be problematic. Go through some of the comments on the first link:
http://west-wind.com/weblog/posts/3016.aspx
http://forums.asp.net/p/1035070/1428363.aspx
http://www.codersbarn.com/post/2008/06/ASPNET-Web-Site-versus-Web-Application-Project.aspx (disclosure, this last link is my own blog)
Try putting the user control in the App_Code folder. Better still, use a Web Application Project instead and you should be able to avoid this issue.
UPDATE
One way around the problem of accessing user controls in website projects is to inline the code then put the user control in the App_Code folder:
http://odetocode.com/blogs/scott/archive/2005/10/01/asp-net-2-0-user-controls-in-app_code.aspx
Personally, this is a sloppy hack at best, and Microsoft do not recommend it:
http://msdn.microsoft.com/en-us/library/t990ks23.aspx
I would create it as a custom server control with a (.cs) and put that in the App_Code folder. This should solve the referencing issue. Ultimately, website projects are not intended for serious work and this is just one reason why. Your best option is to build this as a Web Application project - I can't think of any logical reason for opting for the website template (compile optimizations can also be got with the WAP).
Upvotes: 2
Reputation: 11910
Does that "DateRangePicker.ascx.cs" implement a "CambitCMS.Controls.DateRangePicker" class or a "CambitCMS.DateRangePicker" class? Making sure the naming is right is one point here.
"Controls_DateRangePicker" is not the same as "CambitCMS.Controls.DateRangePicker" is why the compiler is reporting an error. Use the same class name in both files and it should be fine but right now it doesn't appear to be that way.
If the website is set up as a Web Application, it may have to be compiled so that Intellisense can pick up the class name which if you change it in the C# code behind you do have to update the .ascx file is likely where your other error was.
Upvotes: 0