loading usercontrol programatically
![]() |
| |||
![]() |
| |||
@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"%> 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.
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
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"%>
<asp:PlaceHolder ID="PH1" runat="server"></asp:PlaceHolder>C# Code for the "CommentsGroup" control:
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
Hi,
I think Comment control is not well-defined, it should be:
<%
@ 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