I’ve got an ASP.NET web site that includes a usercontrol that’s added programmatically to an ASP.NET web form, using this code:
var control = (Deal)LoadControl("Deal.ascx");
control.ImageFile = dealNode.SelectSingleNode("thumbnail").InnerText;
control.ProductName = dealNode.SelectSingleNode("product").InnerText;
control.PriceText = dealNode.SelectSingleNode("price").InnerText;
DealList.Controls.Add(control);
The Deal control is really simple:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Deal.ascx.cs" Inherits="Deal" %>
<div class="deal">
<img id="DealImage" runat="server" class="dealimage" />
<div class="dealtext"><asp:Label ID="DealLabel" runat="server"></asp:Label></div>
<div class="dealprice"><asp:Label ID="DealPriceLabel" runat="server"></asp:Label></div>
</div>
with the code behind it:
public string ImageFile { get; set; }
public string ProductName { get; set; }
public string PriceText { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
this.DealImage.Src = "images/" + ImageFile;
this.DealLabel.Text = ProductName;
this.DealPriceLabel.Text = PriceText;
}
When I run up the web site in Visual Studio 2010, it usually works fine. However, every so often (usually after a check-in or restarting VS2010), it gives up and won’t compile. I get the error:
The type or namespace name 'Deal' could not be found (are you missing a using directive or an assembly reference?)
The problem being the cast in the line:
var control = (Deal)LoadControl("Deal.ascx");
If I comment out the usercontrol’s Page_Load code, recompile, then uncomment the code and recompile, everything’s OK again.
Can anyone tell me what’s going on?
Try adding a reference to the control inside the page markup :
See here