Problem dynamically loading User Control

Discussion in 'ASP .Net' started by Eric, Feb 10, 2004.

  1. Eric

    Eric Guest

    I'm trying to dynamically load a user control using on the .NET
    framework (not Visual Studio).

    The control was designed in Visual Studio and is named: Disable.ascx
    The first line is:
    <%@ Control Language="c#" className="Disable1" src="Disable.ascx.cs" %>

    The host page for this control is named UserControl.aspx.
    The first 2 lines are:
    <%@ Reference control="Disable.ascx" %>
    <%@ Page language="c#" src="UserControl.aspx.cs" %>

    The code-behind for the host page is called: UserControl.aspx.cs

    The User Control is loaded from this code in the code-behind:
    private void Page_Load(object sender, System.EventArgs e)
    {
    Disable1 uc = (Disable1) Page.LoadControl("Disable.ascx");
    Panel1.Controls.Add(uc);
    }

    I always get this error when I try to bring up the host page:
    CS0246: The type or namespace name 'Disable1' could not be found (are
    you missing a using directive or an assembly reference?)

    and it highlights this line of code:
    Disable1 uc = (Disable1) Page.LoadControl("Disable.ascx");

    How can I get it to dynamically load the user control from the code
    behind file of the host page?

    Thanks,
    Eric Engler
     
    Eric, Feb 10, 2004
    #1
    1. Advertising

  2. By specifying the class name for the user control, you indicate what should
    be used during dynamic compilation of the aspx page. This class name does
    not exist until this page is compiled on the server, so your code behind
    (compiled before you even deploy) is completely unaware of the existence of
    this class. Just take out the casts and insert it as a Control, or else cast
    it specifically to the class name you are loading from.

    --
    Chris Jackson
    Software Engineer
    Microsoft MVP - Windows Client
    Windows XP Associate Expert
    --
    More people read the newsgroups than read my email.
    Reply to the newsgroup for a faster response.
    (Control-G using Outlook Express)
    --

    <Eric> wrote in message news:...
    > I'm trying to dynamically load a user control using on the .NET
    > framework (not Visual Studio).
    >
    > The control was designed in Visual Studio and is named: Disable.ascx
    > The first line is:
    > <%@ Control Language="c#" className="Disable1" src="Disable.ascx.cs" %>
    >
    > The host page for this control is named UserControl.aspx.
    > The first 2 lines are:
    > <%@ Reference control="Disable.ascx" %>
    > <%@ Page language="c#" src="UserControl.aspx.cs" %>
    >
    > The code-behind for the host page is called: UserControl.aspx.cs
    >
    > The User Control is loaded from this code in the code-behind:
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > Disable1 uc = (Disable1) Page.LoadControl("Disable.ascx");
    > Panel1.Controls.Add(uc);
    > }
    >
    > I always get this error when I try to bring up the host page:
    > CS0246: The type or namespace name 'Disable1' could not be found (are
    > you missing a using directive or an assembly reference?)
    >
    > and it highlights this line of code:
    > Disable1 uc = (Disable1) Page.LoadControl("Disable.ascx");
    >
    > How can I get it to dynamically load the user control from the code
    > behind file of the host page?
    >
    > Thanks,
    > Eric Engler
    >
    >
    >
    >
     
    Chris Jackson, Feb 12, 2004
    #2
    1. Advertising

  3. Eric

    Eric Guest

    On Thu, 12 Feb 2004 11:43:40 -0500, "Chris Jackson"
    <chrisjATmvpsDOTorgNOSPAM> wrote:

    >By specifying the class name for the user control, you indicate what should
    >be used during dynamic compilation of the aspx page. This class name does
    >not exist until this page is compiled on the server, so your code behind
    >(compiled before you even deploy) is completely unaware of the existence of
    >this class. Just take out the casts and insert it as a Control, or else cast
    >it specifically to the class name you are loading from.
    >
    >--
    >Chris Jackson
    >Software Engineer
    >Microsoft MVP - Windows Client
    >Windows XP Associate Expert


    Thanks much Chris - you gave me the missing link.

    In the codebehind of the main page I tried casting the control to
    "Disable" instead of "Disable1". But that didn't work because the
    codebehind file of my main page didn't have a referance to the
    codebehind of the control, so it didn't know "Disable".

    So I made a batch file to compile the codebehind for the control and
    put the DLL in the bin dir. Then I made a batch file to compile the
    codebehind of the main page and referance the DLL for the control.

    Then I took out the "SRC=" attributes in both the .ascx and the .aspx,
    since I had compiled them both to DLLs.

    Then it worked!

    Eric
     
    Eric, Feb 12, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    4,358
    Jeremy S.
    Sep 13, 2005
  2. Jamil
    Replies:
    1
    Views:
    2,410
    =?Utf-8?B?SFA=?=
    Nov 24, 2005
  3. Beegee

    Dynamically Loading user control

    Beegee, Jan 14, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    484
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Jan 14, 2006
  4. Replies:
    0
    Views:
    425
  5. =?Utf-8?B?cm9kY2hhcg==?=

    dynamically loading my web user control error

    =?Utf-8?B?cm9kY2hhcg==?=, Apr 17, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    321
    =?Utf-8?B?cm9kY2hhcg==?=
    Apr 17, 2007
Loading...

Share This Page