473,385 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problem dynamically loading User Control

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


Nov 18 '05 #1
2 2439
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:eM*************@TK2MSFTNGP12.phx.gbl...
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

Nov 18 '05 #2
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
Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: marshal | last post by:
Hi to All, I am using <% @ OutputCache Duration="60" VaryByParam="none" %> in my usercontrol say "myControl.ascx". This user control has a property say "title". Now am Dynamically Loading...
7
by: Tim T | last post by:
Hi, I have the need to use dynamically loaded user controls in a webform page. I have the controls loading dynamically, and that part works fine. this is the code used in a webform to dynamically...
1
by: Sundaresan | last post by:
I've a form where I load two user controls dynamically. User Control-1 has a no.of dropdowns and based on the selection I typically populate a datagrid in the user control-2, Also the I could...
1
by: José Joye | last post by:
Hello, I'm playing around with dynamically loading user controls ...and having problems I created a really simple userControl (in fact contains a plain text box) and placed it into the...
1
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am have facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the...
1
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the web...
2
by: A.Wussow | last post by:
Hi Everybody, i want to load dynamically content from some user controls (with forms, or some data-controls) using atlas. So i use an UpdatePanel for loading the user control into a placeholder....
1
by: Bob Rock | last post by:
Hello, I'm new to ASP.NET and I've been looking into the topic of dynamically loading (typically accomplished with a LoadControl followed by a MyControl.Controls.Add()) both user controls and...
5
by: Andrew Robinson | last post by:
I have a page that can load a number of different user controls. Each of these user controls inherits from a common base class and the controls are loaded based on application state, status, etc...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.