Type Conversion for UI.UserControl When its in a Class Library

Oct 8 2012 5:31 AM
I've 3 of .NET Projects.

One of these project is an ASP.Net Web Form Application named Index. And the other projects are listed below
Index.Database Project is Database Layer
Index.Platform Project is Bussiness layer.

In bussiness layer im loading UserControls. There is information bout these UserControls in db. (Path, ascx file, name, Title, Content, Positions etc )

In bussiness layer i created a class drived from UserControl named ModuleControl.

Index.Platform referenced by System.Web.dll also uses Using System.Web.UI.WebControls.

namespace Index.Platform.Modules
{
  public class ModuleControl : System.Web.UI.UserControl
  {
  public string Title { get; set; }
   
  public bool ShowTitle { get; set; }

  public string Content { get; set; }

  public ModuleControl()
  {

  }
  }
}



I'm planning to use this ModuleControl fields in my loaded UserControls. There is another class named IndexSite instanced on Default.aspx's Load_Page event.

//Index.Platform.IndexSite

private void InitializeModules(System.Web.UI.Page page)
  {
  string mPath = null;

  try
  {
  ModuleDictionaryList = DBFactory.LoadModules();

  PositionList = DBFactory.PositionList;
  ModuleList = DBFactory.ModuleList;

  foreach (KeyValuePair<string, List<module>> pair in ModuleDictionaryList)
  {
  foreach (var item in pair.Value)
  {
  mPath = "/Modules/" + item.Name + "/" + item.Name + ".ascx";

  iControl = (ModuleControl)page.LoadControl(mPath);
   
  ShowTitle = Convert.ToBoolean(item.ShowTitle);

  iControl.ShowTitle = ShowTitle;
  iControl.Title = item.Title;
  iControl.Content = item.Content;

  panel = (PlaceHolder)page.Master.FindControl(item.Position);
  panel.Controls.Add(iControl);

  //HttpContext.Current.Response.Write(item.Position + "<br>");
  }
  }
  }
  catch (Exception ex)
  {
  throw new Exception(ex.Message);
  }
  }


this class takes an argument from Default.aspx. Page sends its self.

  protected void Page_Load(object sender, EventArgs e)
  {
  IndexSite site = IndexSite.Instance.Create(this);
  }


Yes, In Bussines layer I use LoadControl method to load USerControl also im adding these controls to a panel control which is in MasterPage (PlaceHolder).

My problem is in this line: iControl = (ModuleControl)page.LoadControl(mPath);

I cant cast UserControl to ModuleControl. Remember this ModuleControl drived from UserControl and all of my ascx file drived from ModuleControl class.

Throws this error: "Unable to cast object of type 'ASP.modules_mod_login_mod_login_ascx' to type 'Index.Platform.Modules.ModuleControl'."

If i do these proccess in Main application there is no error for casting ModuleControl.

When id seperate my application to 3. I stuck here.