I have a user control "ItemNotes" in my application and i am trying to access this control type from a class using following code.
Control ctrl = FindControl("Note1");
if(ctrl is ItemNotes)
{
return ((ItemNotes)ctrl).Value;
}
return "";
It work fine in ASP.NET 1.1 but doesn't compile in 2.0 and i don't see control class in assigned namespace. By default, this class is in App_Code directory and is a base class for other controls.
A workaround is to make ItemNotes a simple class that is derived from UserControl, and then create a separate user control that derives from ItemNotes and use that in the pages.
You can add all the properties/methods that you need to the ItemNotes class (possibly making them virtual or even abstract) and then implement them as needed in the deriving user control class.
This gives you
public class ItemNotes : UserControl
{
...
}
partial class ItemNotesControl : ItemNotes
{
...
}
I can add a reference to a page or another control. But i was trying to access it from a base class file derived from UserControl. This class file is in App_Code directory and working as a base class for other user controls. there were no problems in derived
controls. I cannot access any page or User control in App_Code folder classes. It doesn't compile.
Files in App_code cannot refer classes out side. Whidbey conversion wizad solves this problem by creating an abstract base class in app_code.
This abstract base class is the base class for the classes out side App_code, and code in side app_code can still use it.
Eg:
Myppage.aspx.cs
partial class MyPage : Page
{
public int foo()
{
return 1;
}
}
After conversion
partial class Migrated_MyPage : MyPage
{
override public foo()
{
return 1;
}
}
in app_code new file generated
Stub_Mypage_aspx_cs.cs
abstarct class MyPage : Page
{
abstract public int foo() ;
}
Now code in App_code can refer MyPage and can use public methods if code has access to Migrated_MyPage instance.
"This posting is provided "AS IS" with no warranties, and confers no rights"
in App_code i have a commompage.vb class file and in main solution i have a user control(.ascx).i need to access this user contol into my commonpage.vb class..plz help me out this issue.give a example...
goels
Member
15 Points
3 Posts
How to access a User Control from a Class
Jun 20, 2005 10:44 PM|LINK
Control ctrl = FindControl("Note1");
if(ctrl is ItemNotes)
{
return ((ItemNotes)ctrl).Value;
}
return "";
It work fine in ASP.NET 1.1 but doesn't compile in 2.0 and i don't see control class in assigned namespace. By default, this class is in App_Code directory and is a base class for other controls.
Is there any way to handle this?
DMW
All-Star
15943 Points
2353 Posts
Re: How to access a User Control from a Class
Jun 21, 2005 03:05 AM|LINK
A workaround is to make ItemNotes a simple class that is derived from UserControl, and then create a separate user control that derives from ItemNotes and use that in the pages.
You can add all the properties/methods that you need to the ItemNotes class (possibly making them virtual or even abstract) and then implement them as needed in the deriving user control class.
This gives you
public class ItemNotes : UserControl
{
...
}
partial class ItemNotesControl : ItemNotes
{
...
}
Dave
MrDave
Participant
877 Points
177 Posts
MVP
Re: How to access a User Control from a Class
Jun 21, 2005 04:47 PM|LINK
<%@Reference VirtualPath="ItemNotes.ascx" %>
Read my blog http://blog.davidyack.com
New Silverlight 3 Book - http://www.silverlightjumpstart.com
goels
Member
15 Points
3 Posts
Re: How to access a User Control from a Class
Jun 21, 2005 10:08 PM|LINK
goels
Member
15 Points
3 Posts
Re: How to access a User Control from a Class
Jun 21, 2005 10:40 PM|LINK
I can add a reference to a page or another control. But i was trying to access it from a base class file derived from UserControl. This class file is in App_Code directory and working as a base class for other user controls. there were no problems in derived controls. I cannot access any page or User control in App_Code folder classes. It doesn't compile.
I found another solution as i replied to DMW.
thanks
Saurabh
Baiju
Participant
990 Points
198 Posts
Microsoft
Re: How to access a User Control from a Class
Jun 24, 2005 05:36 PM|LINK
This abstract base class is the base class for the classes out side App_code, and code in side app_code can still use it.
Eg:
Myppage.aspx.cs
partial class MyPage : Page
{
public int foo()
{
return 1;
}
}
After conversion
partial class Migrated_MyPage : MyPage
{
override public foo()
{
return 1;
}
}
in app_code new file generated
Stub_Mypage_aspx_cs.cs
abstarct class MyPage : Page
{
abstract public int foo() ;
}
Now code in App_code can refer MyPage and can use public methods if code has access to Migrated_MyPage instance.
dareddy
Member
2 Points
1 Post
Re: How to access a User Control from a Class
Sep 09, 2011 10:54 AM|LINK
in App_code i have a commompage.vb class file and in main solution i have a user control(.ascx).i need to access this user contol into my commonpage.vb class..plz help me out this issue.give a example...