loading usercontrol programatically

i just used the converter to convert my asp.net 1.1 application to 2.0 and i find that my usercontrols is not working

The type or namespace name 'smartcontrol' could not be found (are you missing a using directive or an assembly reference
At quite a bit of places i am loading the dynamic usercontrols but at some places i am also dragging and dropping the usercontrols to the page as well.I saw some posting but none of them help. this is what i tried. this is the usercontrol


namespace ICE.include
{
 using System;
 using System.Data;
 using System.Drawing;
 using System.Web;
 using System.Web.UI.WebControls;
 using System.Web.UI.HtmlControls;
 using ICE.SMARTMaint;
 using System.Web.UI;
 using System.Text;


 public partial class smartcontrol : System.Web.UI.UserControl
 {
 ....

 }

}

when i dragged and dropped the control onto a new page the htm it spit was this
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="temp.aspx.cs" Inherits="temp" %>

<%@ Register Src="../include/smartcontrol.ascx" TagName="smartcontrol" TagPrefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <uc1:smartcontrol id="Smartcontrol1" runat="server">
        </uc1:smartcontrol>&nbsp;</div>
    </form>
</body>
</html>

in the load event of the page i am trying to acess the control but no intellisence.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class temp : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
  smartcontrol smart = (smartcontrol)Page.FindControl("Smartcontrol1");

  
    }
}
i dont get any intellisence neither can I acess the properties window for the control. can someone help me.
also at many places i am loading the usercontrols dynamically using this
smartcontrol smart =  (smartcontrol)LoadControl("~/include/smartcontrol.ascx");
and in this situation as well i also cant acess the dynamic usercontrol. well i know some postings said add a reference
direrctive and u can acess the properties well if i cant get acess to the usercontrol when i dropped it from the designer
i dont know how i will be to do it in the code.
second question is once i get it figured out why i cant actually get acess to my usercontrol with intellisence after putting reference to it aspx file how will i be able to reference the usercontrol in .cs files in App_code directory. cuz in asp.net 1.1 i had plain .cs files accessing the usercontrol like

public class hello{
private smartcontrol Smart;

 public hello(smartcontrol smart){
Smart = smart;
}

}

since now this class was moved to App_code directory how will it get a reference to the usercontrol where can i put the refernce for this usercontrol like ohter postings were saying about putting referece of the usercontrol in the aspx file cuz i dont have any aspx page.
so if someone could answer my questions it would be great.




Answer this question

loading usercontrol programatically

  • Michael Toner

    I too am having htis exact same problem.  Is there a solution out there
  • JustinHess

    Hi,

    I think Comment control is not well-defined, it should be:

    Code Snippet

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="Comment.ascx.cs" Inherits="Comment" %>

    Anyway, the suitable forum for this kind of questions is http://forums.asp.net. There you should find a better answer for your problem.

    Regards,

    Rodrigo


  • Ozan

    Thanks rodrigo
  • skolima

    I also have used @ Reference but still having the same problem:

    This is "CommentsGroup" control that uses "Comment" control:

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="CommentsGroup.ascx.cs" Inherits="CommentsGroup" %>

    <%@ Reference Control="Comment.ascx"%>

    &nbsp;<asp:PlaceHolder ID="PH1" runat="server"></asp:PlaceHolder>

    C# Code for the "CommentsGroup" control:

    Code Snippet

    using System;

    using System.Data;

    using System.Configuration;

    using System.Collections;

    using System.Web;

    using System.Web.Security;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Web.UI.WebControls.WebParts;

    using System.Web.UI.HtmlControls;

    public partial class CommentsGroup : System.Web.UI.UserControl

    {

    protected void Page_Load(object sender, EventArgs e)

    {

    }

    public void Refresh()

    {

    int i;

    Comment C;

    for (i = 0; i <= 5; i++)

    {

    C=(Comment)LoadControl("~/Comment.ascx");

    PH1.Controls.Add(new Comment());

    }

    }

    }

    and Header of "Comment" control (which is referenced in the "CommentsGroup" control):

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="Comment.ascx.cs" Inherits="WebUserControl" classname="Comment"%>

    When compiling I still receive the Error:

    Error 1 The type or namespace name 'Comment' could not be found (are you missing a using directive or an assembly reference )

    could you please help me

    many thanks in advance



  • RLucky

    For declarative inclusion of control in the page, you used @Register directive at the top of the page. But for programatic there is going to be a change. You will use @Reference directive. This directive takaes only one attribute, Page or Control. The value of this attribute specifies the file that contains the control or the page that this page should link to during dynamic compliation. This step is very important, otherwise your will get Compiler Error CS0246 indicating that class name or type not found.
    <%@ Reference Control="./controls/SiteHeader.ascx"%> 

  • Parripati

    No but the problem i am having is i cant even access usercontrols statically even if i dragged and dropped usercontrols from teh designer. i did some research and found another guy having this same issue.
    http://groups-beta.google.com/group/microsoft.public.dotnet.framework.aspnet/browse_thread/thread/97bf5effde9f1ef0/9dc5354b54109f93 q=dynamic+usercontrol+%2B+asp.net+2.0&rnum=2&hl=en#9dc5354b54109f93


    You can generate the scenario as mentioned below
    I have a project in
    which .cs files (not code-behind) existed in the same namespace as .ascx.cs
    files (codebehinds for user controls).  After upgrading to ASP 2.0, I have
    access to the namespace, but only to the non-codebehind elements in it.  None
    of the classes that exist in codebehind files show up.  It's as if they are
    no longer part of the namespace, even though the namespace is explicitly
    defined in those codebehind files (as was always the case in ASP 1.1).
    so basically i can get acess to .cs files since the got moved over to hte app_code directory but i cant seem to get even static access to my usercontrols. now i am sure i am not the only one who was having this problem. is there a fix for that cuz i cant change the namespaces for my .cs files would be too much work for me. is there a fix for that.


  • Huckster

    In VS05, web form and user controls get compiled into their own assemblies. The source files in the App_Code folder also get compiled into their own assembly. This is why the same namespace does not show your code-behind elements.

    So if you are editing a web form or user control, you won't get intellisense on other web form or user control classes *unless* you add a Reference or Register directive (as mentioned before).

    If you are editing a standalone class file, then it must exist in the App_Code folder and cannot access a web form or user control (i.e. you cannot add a Reference of Register directive). In this case, you need to create an Abstract Base Class (ABC) stub file which exists in the App_Code folder. This will allow your code to compile because it will find the class name in the App_Code assembly, but will allow late-binding to load the proper class at run-time.

    This topic, along with other conversion issues, are discussed in the "common conversion issues" white paper at
    http://msdn.microsoft.com/asp.net/migration/upgrade/default.aspx


  • ogg

    I ran into the same problem.

    When I haphazardly add "ASP" infront of "Comment", it worked.

    Which means, if you change "Comment" into "ASP.Comment", it should work fine.

    That's my experience



  • loading usercontrol programatically